Close Menu
SkytikSkytik

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    At Least 32 People Dead After a Mine Bridge Collapsed Due to Overcrowding

    November 17, 2025

    Here’s how I turned a Raspberry Pi into an in-car media server

    November 17, 2025

    Beloved SF cat’s death fuels Waymo criticism

    November 17, 2025
    Facebook X (Twitter) Instagram
    • About Us
    • Contact Us
    SkytikSkytik
    • Home
    • AI Tools
    • Online Tools
    • Tech News
    • Guides
    • Reviews
    • SEO & Marketing
    • Social Media Tools
    SkytikSkytik
    Home»AI Tools»Following Up on Like-for-Like for Stores: Handling PY
    AI Tools

    Following Up on Like-for-Like for Stores: Handling PY

    AwaisBy AwaisMarch 25, 2026No Comments7 Mins Read0 Views
    Facebook Twitter Pinterest LinkedIn Telegram Tumblr Email
    Following Up on Like-for-Like for Stores: Handling PY
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Introduction

    to my last article, about building the Like-for-Like (L4L) solution based on Power Query:

    The solution works as expected for the most part. I showed it to my peers and to some clients.

    The feedback was positive, but I’ve got some questions, and the results of my solution weren’t what the person asking expected.

    The issue

    I discovered an issue while calculating the PY value.

    Technically, the results are correct, but they aren’t from a user perspective.

    Look at the following two screenshots, which show two different cases that include the Retail Sales and the Retail Sales PY measures. The results for these two cases can confuse the audience.

    Try to spot the issue before continuing to read.

    Figure 1 – The first PY Case – Temporarily closed (Refresh) store (Figure by the Author)

    This is the first case for the Torino store, which was temporarily closed between March and July 2024.

    Figure 2- The second PY case – A mix between a temporarily closed and a Closing store (Figure by the Author)

    And here is the second case for the Roma store, which was temporarily closed from August to October 2023 and permanently closed in August 2024.

    We see these results for the second case:

    1. The values for the Retail Sales PY measure for “Comparable” stores, but with an interruption between August and October.
    2. Values for the Retail Sales measure for “Non-Comparable – Closing” stores.
    3. Values for the Retail Sales PY measure for “Non-Comparable – Refresh” stores.

    From a technical point of view, these results make absolute sense and are correct.

    The measures show the correct L4L States for the current period and the previous year.

    So, what are the issues?

    For the user, they are very confusing and will not match expectations.

    Think about it from the user’s perspective:

    When looking at results for specific L4L states, the two measures should assign results to the same L4L state, regardless of whether they are calculated for the current period or the previous year.

    This introduces a new complexity to the solution.

    The solution

    I need a second column for the L4LKey for the previous year.

    For the first L4LKey column, I compare the opening and closing dates to the monthly dates of the previous year (See the first article for the details).

    For the second L4LKey_PY column, I must compare these dates to the monthly dates of the same year as the opening and closure dates.

    The idea is somewhat counterintuitive, but it delivers the result I need.
    Please stay with me, and you will see how it pans out

    First, I tried solving it in Power Query, as I did in the original solution. But it didn’t work. I will come to the reason in a minute.

    Then, I switched to building the Bridge_L4L table in SQL, but the results were unusable again, as I always got duplicated rows for the Rome store, as I have two rows for the two L4L-states for this store:

    Figure 3 – Two rows for the Rome store (ID 222) for the two years 2023 and 2024 (Figure by the Author)

    I have one row each for the temporary closure in 2023 and the definitive closure in 2024.

    Therefore, the join always returns two rows, as the store key is duplicated.

    So, I decided to switch to a procedural approach.

    I loop through each row in the table containing the opening and closing stores and apply the states to the table, which has one row per store and month.

    I did this by using temporary tables in SQL and the following SQL code:

    -- Declare all needed variables
    DECLARE @StoreKey       int;
    DECLARE @OpenDate       date;
    DECLARE @CloseDate      date;
    DECLARE @L4LKey         int;
    
    -- Create the Cursor to loop through the Stores with each opening, closing, and refresh dates
    DECLARE sd CURSOR FOR
        SELECT [StoreKey]
                ,[OpenDate]
                ,[CloseDate]
                ,[L4LKey]
            FROM #tmp_Store_Dates
                -- Order per Closing date, as the procedure must run from the first (oldest) to the last (newest) row
                ORDER BY [CloseDate];
    
    OPEN sd;
    
    -- Get the first row
    FETCH NEXT FROM sd INTO @StoreKey, @OpenDate, @CloseDate, @L4LKey;
    
    -- Start the loop
    WHILE @@FETCH_STATUS = 0
    BEGIN
        -- Update all rows according to each store based on the L4L status and the respective dates, based on the previous years' dates
        UPDATE [#tmp_Stores_Months]
            SET [OpenDate] = @OpenDate
                ,[CloseDate] = @CloseDate
                ,[L4LKey] = CASE @L4LKey
                                WHEN 2
                                    THEN IIF(@OpenDate >= [FirstDayOfMonthPY], @L4LKey, NULL)
                                WHEN 3
                                    THEN IIF(@CloseDate <= [LastDayOfMonthPY], @L4LKey, NULL)
                                WHEN 4
                                    THEN IIF(@OpenDate >= [FirstDayOfMonthPY] AND @CloseDate <= [LastDayOfMonthPY], @L4LKey, NULL)
                                    ELSE 1
                                END
                WHERE [L4LKey] IS NULL
                    AND [StoreKey] = @StoreKey;
    
    -- Update based on the same month for the PY calculation
    UPDATE [#tmp_Stores_Months]
            SET [OpenDate] = @OpenDate
                ,[CloseDate] = @CloseDate
                ,[L4LKey_PY] = CASE @L4LKey
                                WHEN 2
                                    THEN IIF(@OpenDate >= [FirstDayOfMonth], @L4LKey, NULL)
                                WHEN 3
                                    THEN IIF(@CloseDate <= [LastDayOfMonth], @L4LKey, NULL)
                                WHEN 4
                                    THEN IIF(@OpenDate >= [FirstDayOfMonth] AND @CloseDate <= [LastDayOfMonth], @L4LKey, NULL)
                                    ELSE 1
                                END
                WHERE [L4LKey_PY] IS NULL
                    AND [StoreKey] = @StoreKey;
        
        -- Get the next row until all rows are processed
        FETCH NEXT FROM sd INTO @StoreKey, @OpenDate, @CloseDate, @L4LKey;
    
    END
    
    -- Close the Cursor
    CLOSE sd;
    DEALLOCATE sd;
    
    -- Update the L4LKey and L4LKey_PY in all empty rows
    UPDATE #tmp_Stores_Months
        SET [L4LKey] = 1
            WHERE [L4LKey] IS NULL;
    
    UPDATE #tmp_Stores_Months
        SET [L4LKey_PY] = 1
            WHERE [L4LKey_PY] IS NULL;

    The result of the procedure is a table containing one column mapping the L4L states based on the previous year for each month (L4LKey) and one column mapping the L4L states based on the same year for each month (L4LKey_PY):

    Figure 4 – The result of the procedure for the Bridge_L4L table with the two L4LKey columns (Figure by the Author)

    The next step is to import the result for this procedure into Power BI and add an additional relationship between the Bridge_4L and the DIM_L4L table for the new L4LKey_PY column:

    Figure 5 – The datamodel with the additional L4LKey_PY column and the additional relationship to DIM_L4L (Figure by the Author)

    This allows me to control the calculation for the PY result.

    Retail Sales (PY) =
    CALCULATE([Retail Sales]
                ,'Time Intelligence'[Time Measures] = "PY"
                ,USERELATIONSHIP('Bridge_L4L'[L4LKey_PY], 'DIM_L4L'[L4LKey])
                )

    Now, the results are what is expected.

    Here, the first case:

    Figure 6 – The results for the Rome store for 2024. Now the results are consistent (Figure by the Author)

    And here are the results for the second case:

    Figure 7 – The consistent results for the store for 2025 (Figure by the Author)

    As you can see, the PY values are assigned to the same L4L state as the current-year results.

    Now, the user sees consistent results, which are much easier to understand.

    Conclusion

    The additional call of the USERELATIONSHIP() function can be put in a Calculation Item and used by all PY measures.

    This makes it very easy to use without any additional DAX logic.

    Anyway, this challenge was relatively easy to solve. But when I considered a Month-over-Month calculation with the L4L functionality, I realized it wouldn’t be possible without some DAX code. Possibly, I will dig into this in a future article.

    But this case emphasizes the need to use the user’s perspective when designing and testing a solution.

    It isn’t enough to use a technical perspective; the user’s perspective is much more important when evaluating the solution’s functionality and results.

    For me, this was a very interesting experience and very useful for my future work.

    I hope that you find my approach interesting. Stay tuned for my next piece.

    References

    This is my previous article on this topic:

    Here is the SQLBI article about the like-for-like pattern with a DAX solution based on model-independent UDFs.

    Like in my previous articles, I use the Contoso sample dataset. You can download the ContosoRetailDW Dataset for free from Microsoft here.

    The Contoso Data can be used freely under the MIT License, as described in this document. I updated the dataset to shift the data to contemporary dates and removed all tables not needed for this example.

    handling LikeforLike Stores
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Awais
    • Website

    Related Posts

    [2603.23073] Can an LLM Detect Instances of Microservice Infrastructure Patterns?

    March 25, 2026

    [2603.03147] Agentic AI-based Coverage Closure for Formal Verification

    March 25, 2026

    Building Human-In-The-Loop Agentic Workflows | Towards Data Science

    March 25, 2026

    A Multi-Provider Framework for Automated PEGS Analysis Across Software Domains

    March 25, 2026

    [2503.10144] Multiplicative learning from observation-prediction ratios

    March 25, 2026

    Sim-to-Real of Humanoid Locomotion Policies via Joint Torque Space Perturbation Injection

    March 25, 2026
    Leave A Reply Cancel Reply

    Top Posts

    At Least 32 People Dead After a Mine Bridge Collapsed Due to Overcrowding

    November 17, 20250 Views

    Here’s how I turned a Raspberry Pi into an in-car media server

    November 17, 20250 Views

    Beloved SF cat’s death fuels Waymo criticism

    November 17, 20250 Views
    Don't Miss

    A playbook for machine-readable content

    March 25, 2026

    Once upon a time, in the delightfully chaotic 1990s, web copywriting was all about exact-match…

    [2603.23073] Can an LLM Detect Instances of Microservice Infrastructure Patterns?

    March 25, 2026

    Safely automate OpenClaw with Zapier MCP

    March 25, 2026

    Amazon’s Spring Sale Is Like a Bonus Prime Day

    March 25, 2026
    Stay In Touch
    • Facebook
    • YouTube
    • TikTok
    • WhatsApp
    • Twitter
    • Instagram
    Latest Reviews

    [2603.03147] Agentic AI-based Coverage Closure for Formal Verification

    March 25, 2026

    How to copy and paste in Google Docs: 3 methods

    March 25, 2026
    Most Popular

    13 Trending Songs on TikTok in Nov 2025 (+ How to Use Them)

    November 18, 20257 Views

    How to watch the 2026 GRAMMY Awards online from anywhere

    February 1, 20263 Views

    Corporate Reputation Management Strategies | Sprout Social

    November 19, 20252 Views
    Our Picks

    At Least 32 People Dead After a Mine Bridge Collapsed Due to Overcrowding

    November 17, 2025

    Here’s how I turned a Raspberry Pi into an in-car media server

    November 17, 2025

    Beloved SF cat’s death fuels Waymo criticism

    November 17, 2025

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    Facebook X (Twitter) Instagram Pinterest YouTube Dribbble
    • About Us
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    • Disclaimer

    © 2025 skytik.cc. All rights reserved.

    Type above and press Enter to search. Press Esc to cancel.