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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
RodgerB
Frequent Visitor

Calculating YTD Sales using a Variable

Good afternoon, Team,

I'm trying to do what should be a very simple thing.  Use variables in a CALCULATE for DATESYTD, and when I add it to a visual, it just shows the sum by row.

My DAX is:

 

YTD Sales = VAR Sales = SUM([Sale AMT])
                    VAR Dates = DATESYTD('Calendar'[Date])
                    VAR Results = CALCULATE(Sales, Dates)
                    Return Results

I have created a date table, and it is both marked as the date table and related properly. 

Can anyone let me know what I'm doing wrong, or why this isn't returning YTD summarizations?  Thanks!


1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

You should not calculate your expression first as a variable.  The way it is writtten, it is first doing the sum in the current context and then passing that as a fixed value into the Calculate() part.  Try this instead:

 

YTD Sales = CALCULATE(SUM(Table[Sale AMT]), DATESYTD('Calendar'[Date']) //replace Table with the name of your table

 

This assumes you are using columns from your 'Date' table in your visual.

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

3 REPLIES 3
mahoneypat
Microsoft Employee
Microsoft Employee

You should not calculate your expression first as a variable.  The way it is writtten, it is first doing the sum in the current context and then passing that as a fixed value into the Calculate() part.  Try this instead:

 

YTD Sales = CALCULATE(SUM(Table[Sale AMT]), DATESYTD('Calendar'[Date']) //replace Table with the name of your table

 

This assumes you are using columns from your 'Date' table in your visual.

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Good evening, Pat,

 

I am well versed on YTD using CALCULATE and DATESYTD, as well as using DATEADD to get the previous year.  As I'm trying to work with VAR and RESULT now, I was just hoping to use them to make the syntax for YTD sales easier to read for my end users.

Thanks!

Rodger

 

@mahoneypat 

@RodgerB You did it? I have the same problem

 

 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.