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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

DAX calculation based on Start Date and End Date

Hello everyone,

 

Need some help on some DAX calculation as I have very little knowledge about it.

So, I got this kind of data :

medwong_0-1607102978760.png

Then I added a Date Slicer which has Start and End Date like this :

medwong_1-1607103021252.png

How is the DAX formula looks like if I want to have :

1. Cost value at the beginning of the period selected (ex: 3/28/2020)

2. Cost value at the end of the period selected (ex: 8/29/2020)

 

Appreciate any help.

 

Thanks before.

 

1 ACCEPTED SOLUTION
mahoneypat
Employee
Employee

Please try these two measure expressions in Card visuals.

 

Starting Sum = var vMinDate = MIN(Table[Date])
return Calculate(SUM(Table[Cost]), FILTER(ALL(Table[Date]), Table[Date]<= vMinDate))

 

Ending Sum = SUM(Table[Cost])

 

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

6 REPLIES 6
mahoneypat
Employee
Employee

Yes.  The MIN is getting the min date in current context, which is defined by your slicer.  SELECTEDVALUE can be used only when there is a single value in scope.  Even when a single value is in scope, MIN/MAX/SUM/AVG are still often used instead of SELECTEDVALUE to harvest the value for calculations.

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


Anonymous
Not applicable

Many thanks for your guidance. 

mahoneypat
Employee
Employee

Please try these two measure expressions in Card visuals.

 

Starting Sum = var vMinDate = MIN(Table[Date])
return Calculate(SUM(Table[Cost]), FILTER(ALL(Table[Date]), Table[Date]<= vMinDate))

 

Ending Sum = SUM(Table[Cost])

 

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


Anonymous
Not applicable

Hi @mahoneypat 

 

Many thanks. I tried and it is good. Just for my better understanding, when we have this code "var vMinDate = MIN(Table[Date])", doesn't it need to have like SELECTED or SELECTEDVALUE or we can actually just do it with what you've been described ?

 

I know it is working without it, but since I have slicer for the date, just wondering if just Min(Table[Date]) will refer to the slicer or not. Again it is just for my better understanding. I actually still confuse on this DAX features and how it works.

 

Thanks again.

 

 

mahoneypat
Employee
Employee

You can use LASTNONBLANKVALUE or FIRSTNONBLANKVALUE.  For example,

 

Min Cost = FIRSTNONBLANKVALUE(Table[Date], MIN(Table[Cost]))

 

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


Anonymous
Not applicable

Hi @mahoneypat 

 

Sorry, I didn't get what you;re meaning. But gotta the feeling I probably didn;t explain correctly.

So what my intention is, as that is my inventory data, I want to have my inventory value at the beginning and end of the selected date.

 

I probably wrong, but I thought we have to do some CALCULATE function for totaling all the item's cost ? So on my mind is something like SUM through my table from the earliest transaction up to date specified in the Start Date, and then SUM through my table again but this time from earliest up to End Date. 

 

Refer to your suggestion, it looks like we only to get what is the cost of any item that happened on the Start or End Date, is it not ?

 

Thanks,

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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