Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

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

1 ACCEPTED SOLUTION
ValtteriN
Super User
Super User

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:
ValtteriN_0-1642856879617.png


End result:

ValtteriN_1-1642856900642.png

 







Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

6 REPLIES 6
ValtteriN
Super User
Super User

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:
ValtteriN_0-1642856879617.png


End result:

ValtteriN_1-1642856900642.png

 







Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




ValtteriN
Super User
Super User

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!






Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




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?

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.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Anonymous
Not applicable

Hi, thank you again. I guess u r right there might be something with the original data. I don't see any null value or data type issue but when I reimported the original date data it was solved. Thank you very much again!

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.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.