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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
freginier
Impactful Individual
Impactful Individual

Dynamic calculated column

Dear all, 

 

I use this calculated column to get the value from 5 days ago for each row. The formula works very well but now I would be able to replace the number of day by variable. 

 

I try to use parameter table but I can't capture slicer selected value in a column.. How can I do ? 

 

 

Previous Value (5) = 
VAR __PreDate = Transaction[Date] - 5
VAR __PreValue = MAXX(FILTER(Transaction,Transaction[Date] = __PreDate && Transaction[Group] = EARLIER(Transaction[Group])),Transaction[Value])
return __PreValue

 

 

Thank you 

 

 

1 ACCEPTED SOLUTION

@freginier Oh, you can use EARLIER in a measure by using ADDCOLUMNS. But in your case I would just do this:

Previous Value (5) = 
VAR __TransactionGroup = MAX('Transaction'[Group])
VAR __PreDate = MAX(Transaction[Date]) - 5
VAR __PreValue = MAXX(FILTER(Transaction,Transaction[Date] = __PreDate && Transaction[Group] = __TransactionGroup ),Transaction[Value])
return __PreValue

More or less this pattern:

See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395....

 

Also, the Quick Measure Gallery entry demonstrates using EARLIER in a measure: (2) Mean Time Between Failure (MTBF) - Microsoft Power BI Community
The basic pattern is:
Column = 
  VAR __Current = [Value]
  VAR __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])

  VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
RETURN
  __Current - __Previous



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

3 REPLIES 3
Greg_Deckler
Super User
Super User

@freginier Calculated columns are not dynamic, they are only calculated at the time of refresh. You likely need to use a measure instead.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler  I know that I need to use a Measure but how to convert my column to Measure ?

How to use earlier in a measure ? 😊

@freginier Oh, you can use EARLIER in a measure by using ADDCOLUMNS. But in your case I would just do this:

Previous Value (5) = 
VAR __TransactionGroup = MAX('Transaction'[Group])
VAR __PreDate = MAX(Transaction[Date]) - 5
VAR __PreValue = MAXX(FILTER(Transaction,Transaction[Date] = __PreDate && Transaction[Group] = __TransactionGroup ),Transaction[Value])
return __PreValue

More or less this pattern:

See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395....

 

Also, the Quick Measure Gallery entry demonstrates using EARLIER in a measure: (2) Mean Time Between Failure (MTBF) - Microsoft Power BI Community
The basic pattern is:
Column = 
  VAR __Current = [Value]
  VAR __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])

  VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
RETURN
  __Current - __Previous



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
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! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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