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
mstefancik
Advocate IV
Advocate IV

Sum based on expression

Hello, could anybody help me to rewrie SQL command to DAX. I woud like to sum column from table invoice based on two dates columns difference and divide the results into bins, based on dates difference.

 

SQL query:

 

SELECT
sum(CASE WHEN Date2-Date1 < 7 THEN value END) AS '0-7',
sum(CASE WHEN Date2-Date1 < 14 THEN value END) AS '7-14',
sum(CASE WHEN Date2-Date1 < 30 THEN value END) AS '14-30',
sum(CASE WHEN Date2-Date1 > 30 THEN value END) AS '>30'
FROM invoice

 

 

Thanks

 

1 ACCEPTED SOLUTION
v-qiuyu-msft
Community Support
Community Support

Hi @mstefancik,

 

In your scenario, you can create a calculated column to display days differences, then create another column to display values based on this difference column. After that, you can place those columns in a matrix visual. Please refer to screenshots below:

 

Date2-Date1 = DATEDIFF(Table2[Date1],Table2[Date2],DAY)

 

Column = Switch(true(),Table2[Date2-Date1]>30,">30",Table2[Date2-Date1]<=30 && Table2[Date2-Date1]>14,"14-30",Table2[Date2-Date1]<=14 && Table2[Date2-Date1]>7,"7-14",Table2[Date2-Date1]<=7,"0-7",BLANK())

 

a1.PNGa2.PNG

 

 

If you have any question, please feel free to ask.

 

Best Regards,
Qiuyun Yu

Community Support Team _ Qiuyun Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-qiuyu-msft
Community Support
Community Support

Hi @mstefancik,

 

In your scenario, you can create a calculated column to display days differences, then create another column to display values based on this difference column. After that, you can place those columns in a matrix visual. Please refer to screenshots below:

 

Date2-Date1 = DATEDIFF(Table2[Date1],Table2[Date2],DAY)

 

Column = Switch(true(),Table2[Date2-Date1]>30,">30",Table2[Date2-Date1]<=30 && Table2[Date2-Date1]>14,"14-30",Table2[Date2-Date1]<=14 && Table2[Date2-Date1]>7,"7-14",Table2[Date2-Date1]<=7,"0-7",BLANK())

 

a1.PNGa2.PNG

 

 

If you have any question, please feel free to ask.

 

Best Regards,
Qiuyun Yu

Community Support Team _ Qiuyun Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@v-qiuyu-msft thank you very much, it works like a charm!

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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