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
Mathias_W
Regular Visitor

Looping a working DAX function in a calculated column

Hi All,

I have a function that will look at a null field in column "Project Commit Cap" and lookup the value with the same ID and the next earlier date and give column "Project Commit Cap NoNulls" that value. DAX does not do it recursively so if there are 2 null values in a row I get a blank. In another language a simple while loop (while value is null do: XYZ) would solve this, but DAX does not support For/While loops?

 

How can i get the working DAX function to apply the filled in values and keep running until there are no more nulls?

Optionally how can i brute force it to run X number of times (maybe 10)? it wouldn't be a pretty solution but will get the job done

Example tables below are filtered to show only one 2 IDs, records of different IDs are between the records shown here so I cant do an excel-like function "take value from row below"

 

Here is the dax function that will fill in the previous value:

 

'ProjectCommitCap_NoNulls =
if(isblank('Project Story Data'[Projected Commitments Cap]),
LOOKUPVALUE('Project Story Data'[Projected Commitments Cap],'Project Story Data'[ID],'Project Story Data'[ID],
'Project Story Data'[Date of Data],CALCULATE(MAX('Project Story Data'[Date of Data]),FILTER('Project Story Data','Project Story Data'[ID]=EARLIER('Project Story Data'[ID])&&'Project Story Data'[Date of Data]<EARLIER('Project Story Data'[Date of Data])))),
'Project Story Data'[Projected Commitments Cap])'

 

Here is what the table looks like currently (only filling in one blank value gap:

IDDate of DataProjected Commitments CapProjectCommitCap_NoNulls
31894/1/2019 $28,252.99
31892/25/2019$28,252.99$28,252.99
31891/14/2019  
318912/24/2018 $27,820.85
318911/5/2018$27,820.85$27,820.85
318910/22/2018 $28,036.92
318910/15/2018$28,036.92$28,036.92
248510/15/2018 $11,114.51
248510/9/2018$11,114.51$11,114.51
318910/9/2018  
31899/24/2018  
31899/10/2018  
31899/4/2018 $33,100,567.50
31898/27/2018$33,100,567.50$33,100,567.50
31898/13/2018 $31,229.54
31897/23/2018$31,229.54$31,229.54
31897/16/2018 $28,896.80
31897/9/2018$28,896.80$28,896.80
31896/25/2018$29,790.51$29,790.51
31896/18/2018 $0.00
31896/11/2018$0.00$0.00


Here is what I want it to look like:

IDDate of DataProjected Commitments CapProjectCommitCap_NoNulls
31894/1/2019 $28,252.99
31892/25/2019$28,252.99$28,252.99
31891/14/2019 $27,820.85
318912/24/2018 $27,820.85
318911/5/2018$27,820.85$27,820.85
318910/22/2018 $28,036.92
318910/15/2018$28,036.92$28,036.92
248510/15/2018 $11,114.51
248510/9/2018$11,114.51$11,114.51
318910/9/2018 $33,100,567.50
31899/24/2018 $33,100,567.50
31899/10/2018 $33,100,567.50
31899/4/2018 $33,100,567.50
31898/27/2018$33,100,567.50$33,100,567.50
31898/13/2018 $31,229.54
31897/23/2018$31,229.54$31,229.54
31897/16/2018 $28,896.80
31897/9/2018$28,896.80$28,896.80
31896/25/2018$29,790.51$29,790.51
31896/18/2018 $0.00
31896/11/2018$0.00$0.00

 

Thanks! Let me know if i need to provide more detail 🙂

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

Looping is generally not permitted in DAX. However, I have written some blog articles on simulating looping in DAX that you might find useful:

 

https://community.powerbi.com/t5/Community-Blog/For-and-While-Loops-in-DAX/ba-p/636314

https://community.powerbi.com/t5/Community-Blog/Previous-Value-Recursion-in-DAX/ba-p/638320


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

1 REPLY 1
Greg_Deckler
Super User
Super User

Looping is generally not permitted in DAX. However, I have written some blog articles on simulating looping in DAX that you might find useful:

 

https://community.powerbi.com/t5/Community-Blog/For-and-While-Loops-in-DAX/ba-p/636314

https://community.powerbi.com/t5/Community-Blog/Previous-Value-Recursion-in-DAX/ba-p/638320


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.