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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
TYL001
Frequent Visitor

DAX to calculate total using prior calculated total based on category

Hi - 

I have below data in Table View and need a Dax formula to compute a column with the total based on prior week's computed number (prior total * factor) for each category:

 

CategoryDatefactorTotalcalculation
A01/01/24                  -  100 
A01/08/24      1.01682101.68(100*1.01682)
A01/15/24      1.01734103.45(101.68*1.01734)
A01/22/24      1.01939105.45(103.45*1.01939)
A01/29/24      1.01854107.41(105.45*1.01854)
B01/01/24      1.0179250 
B01/08/24      1.0161250.81 
B01/15/24      1.0133251.48 
B01/22/24      1.0113452.07 
B01/29/24      1.0116652.67 
2 ACCEPTED SOLUTIONS
xifeng_L
Super User
Super User

Hi @TYL001 ,

 

You can try below column expression.

 

xifeng_L_0-1715962343177.png

 

Column = 
VAR TempTable = FILTER('Table','Table'[Category]=EARLIER('Table'[Category]) && 'Table'[Date]<=EARLIER('Table'[Date]))
VAR FirstVal = MAXX(TOPN(1,TempTable,'Table'[Date],1),'Table'[Total])
RETURN
PRODUCTX(
    TempTable,
    VAR MinDateOfCat = MINX(TempTable,'Table'[Date])
    RETURN
    IF('Table'[Date]=MinDateOfCat,1,'Table'[factor])
)*FirstVal

 

 

Did I answer your question? If yes, pls mark my post as a solution and appreciate your Kudos !

 

Thank you~

 

 

View solution in original post

TYL001
Frequent Visitor

Answered!

Column = 
VAR TempTable = FILTER('Table','Table'[Category]=EARLIER('Table'[Category]) && 'Table'[Date]<=EARLIER('Table'[Date]))
VAR FirstVal = MAXX(TOPN(1,TempTable,'Table'[Date],1),'Table'[Total])
RETURN
PRODUCTX(
    TempTable,
    VAR MinDateOfCat = MINX(TempTable,'Table'[Date])
    RETURN
    IF('Table'[Date]=MinDateOfCat,1,'Table'[factor])
)*FirstVal

View solution in original post

5 REPLIES 5
TYL001
Frequent Visitor

Answered!

Column = 
VAR TempTable = FILTER('Table','Table'[Category]=EARLIER('Table'[Category]) && 'Table'[Date]<=EARLIER('Table'[Date]))
VAR FirstVal = MAXX(TOPN(1,TempTable,'Table'[Date],1),'Table'[Total])
RETURN
PRODUCTX(
    TempTable,
    VAR MinDateOfCat = MINX(TempTable,'Table'[Date])
    RETURN
    IF('Table'[Date]=MinDateOfCat,1,'Table'[factor])
)*FirstVal
xifeng_L
Super User
Super User

Hi @TYL001 ,

 

You can try below column expression.

 

xifeng_L_0-1715962343177.png

 

Column = 
VAR TempTable = FILTER('Table','Table'[Category]=EARLIER('Table'[Category]) && 'Table'[Date]<=EARLIER('Table'[Date]))
VAR FirstVal = MAXX(TOPN(1,TempTable,'Table'[Date],1),'Table'[Total])
RETURN
PRODUCTX(
    TempTable,
    VAR MinDateOfCat = MINX(TempTable,'Table'[Date])
    RETURN
    IF('Table'[Date]=MinDateOfCat,1,'Table'[factor])
)*FirstVal

 

 

Did I answer your question? If yes, pls mark my post as a solution and appreciate your Kudos !

 

Thank you~

 

 

@xifeng_L You. are. AMAZING. Thank you, Thank you!! 🍪

Greg_Deckler
Super User
Super User

@TYL001 I will see if I can give this a try but this looks very much like recursion and DAX hates recursion. It *might* be possible with PREVIOUS and visual calculations.

 



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 Thanks for taking a stab at this.  I was trying to mimic the solution to another post I found (but it had no category), but the code didn't work out (returning NaN). Below is what I tried:

 

CALCULATION = 
VAR _MINVALUE = 
DATA[Total]
    *CALCULATE(
        SUM(DATA[factor]),
        FILTER(DATA,DATA[Date]=MIN('DATA'[Date]])&&DATA[Category]=DATA[Category])
    )
        RETURN
        IF(
            DATA[Date]=MIN('DATA'[Date]])&&DATA[Category]=DATA[Category],
            _MINVALUE,
            CALCULATE(
                PRODUCT(DATA[factor]),
                FILTER(DATA,DATA[Date]<=EARLIER(DATA[Date])&&DATA[Category]=DATA[Category])
        )*DATA[factor]
        )

 

 

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!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.