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 calculate if datedif between slicer selected date and column date equal to value

I'm making calculated buckets based on the date difference. Currently, the bucket is generated based on the last date of the current month as below measure.

 

Measure =CALCULATE(SUM(RF[Cost]),DATEDIFF(EOMONTH(TODAY(),0),RF[PoDate],DAY)<30)
 
Basically this gives sum of the cost if the count of the dates between the current month end date and podate lesser than 30. But i need EOMONTH(TODAY(),0) to be replaced with a user-selected value using a slicer or any other method. 
 
Additionally, I have created 
Parameter = GENERATESERIES(DATE(2021,01,01),DATE(2023,01,01), 1)
Parameter Value = SELECTEDVALUE('Parameter'[Parameter])
where user to select the date using slicer. But when i replace Parameter value with EOMONTH(TODAY(),0) like below
 
Measure = CALCULATE(SUM(BIItemLedgerRF[CostAmountActual]),DATEDIFF(MIN(Parameter[Parameter Value]),BIItemLedgerRF[PoDate.PoDate],DAY)<30)
 
But It generates an error by telling Column 'Parameter Value' in table 'Parameter' cannot be found or may not be used in this expression.
 
Please help me to sort this out struggling with this for a long time.
1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@Anonymous , make sure the names are correct.

 

Also try like , You need have some grouping level for this. I assumed - BIItemLedgerRF[Bill ID]

Measure =
var _min =MINX(allselected(Parameter),Parameter [Parameter Value])
return
CALCULATE(SUM(BIItemLedgerRF[CostAmountActual]),filter(values(BIItemLedgerRF[Bill ID]) , DATEDIFF(_min,max(BIItemLedgerRF[PoDate.PoDate]),DAY)<30))

 

 

You need to get dynamic bucketing. Dynamic Segmentation, Bucketing or Binning: https://youtu.be/CuczXPj0N-k

 

also refer date diff across table

https://community.powerbi.com/t5/Community-Blog/Decoding-Direct-Query-in-Power-BI-Part-2-Date-Difference-Across/ba-p/934397#M451

 

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

@Anonymous 

 

It can't be used in this expression because logical expressions under CALCULATE can't contain measures. They can only be of this form (more or less):

 

F(T[Col]) <operator> Value,

 

where Value is a constant and F is a function that depends only on one column in a base table, never measures.

 

Therefore, first capture the value of the measure in a variable and then use the variable in a well-formed expression. And you don't need any MIN around the parameter value. You should also follow Best Practices when formatting and writing DAX code. Otherwise, you'll get into trouble rather sooner than later.

 

Rules for DAX Code Formatting - SQLBI

Anonymous
Not applicable

@Anonymous  Found the solution from another user.  Till today I have not followed formating since I was transferred to DAX from M query very recently. Thank you for aware me of best practising on DAX code and for the reference. 

amitchandak
Super User
Super User

@Anonymous , make sure the names are correct.

 

Also try like , You need have some grouping level for this. I assumed - BIItemLedgerRF[Bill ID]

Measure =
var _min =MINX(allselected(Parameter),Parameter [Parameter Value])
return
CALCULATE(SUM(BIItemLedgerRF[CostAmountActual]),filter(values(BIItemLedgerRF[Bill ID]) , DATEDIFF(_min,max(BIItemLedgerRF[PoDate.PoDate]),DAY)<30))

 

 

You need to get dynamic bucketing. Dynamic Segmentation, Bucketing or Binning: https://youtu.be/CuczXPj0N-k

 

also refer date diff across table

https://community.powerbi.com/t5/Community-Blog/Decoding-Direct-Query-in-Power-BI-Part-2-Date-Difference-Across/ba-p/934397#M451

 

Anonymous
Not applicable

The method you mentioned has worked. Additionally by following the references you mentioned, found this article https://www.credera.com/insights/creating-aging-report-using-a-user-selected-date-in-power-bi on how to handle bucketing.

 

Thank you @amitchandak 

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.

Top Solution Authors