Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

How to turn multiple SQL joins into DAX? Example challenge

Help will be very much appreciated!

 

Say if I want to create a measure called "Prior_Sales", now I have two tables I can use - [dim_date] and [sales].

The result should be: given a particular ISO_Year_Week ("202219", meaning ISO Year 2022, week 19), I want to show the sales aggregated for ISO year 2011, week 19.

 

Here is my SQL code:

 

SELECT

iso_week,
c.iso_year_week,
Sum(sold_quantity) AS Prior_Sales
FROM [sales] a
JOIN

(SELECT calendar_date, iso_year_week FROM [dim_date] WHERE current_fin_year = 'Previous') b
ON a.calendar_date = b.calendar_date
RIGHT JOIN

(SELECT DISTINCT iso_week, iso_year_week FROM [dim_date] WHERE current_fin_year = 'current') c

-- using right join because ISO week are sometimes 52, sometimes 53
ON a.iso_week = c.iso_week
GROUP BY iso_week,
c.iso_year_week
ORDER BY 2

  • Hi,

    In the case of comparing weeks you could use variables.
    If your data format is like you described somethin like this should do:
    e.g. [measure] =

    var _ISOWEEK = MAX(Isoweeks[ISOWeek])
    var _LY_ISOWEEK = CONCATENATE(LEFT(_ISOWEEK,4)-1,RIGHT(_ISOWEEK,2))
    return

    CALCULATE(SUM(Isoweeks[Value]),ALL(Isoweeks),Isoweeks[ISOWEEK]=_LY_ISOWEEK)

    Example data:


    End result:

     



6 Replies

  • ValtteriN's avatar
    ValtteriN
    Community Champion

    Hi,

    For calculating previous years values you can use SAMEPERIODLASTYEAR


    [Sales LY]=

    CALCULATE([Sales],SAMEPERIODLASTYEAR('Calendar'[Date]))

    I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!


    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi, 

      Thanks for your response and this is a great formula to know!

      The issue here is that the requirement is to look at the same "ISO Weeks", which are usually not the same dates and it's what complicates the problem.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Valterri, I just tried this formula and encountered a strange error prompt "an invalid numeric representation of a date value was encountered", do you happen to know what does it mean?

      • ValtteriN's avatar
        ValtteriN
        Community Champion

        Hi,

        I would check whether or not you have NULL values and also what is the data format of your columns. These might be the reason for this error.

  • ValtteriN's avatar
    ValtteriN
    Community Champion

    Hi,

    In the case of comparing weeks you could use variables.
    If your data format is like you described somethin like this should do:
    e.g. [measure] =

    var _ISOWEEK = MAX(Isoweeks[ISOWeek])
    var _LY_ISOWEEK = CONCATENATE(LEFT(_ISOWEEK,4)-1,RIGHT(_ISOWEEK,2))
    return

    CALCULATE(SUM(Isoweeks[Value]),ALL(Isoweeks),Isoweeks[ISOWEEK]=_LY_ISOWEEK)

    Example data:


    End result: