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
CupidC
Helper II
Helper II

Cumulative Column in a Query Model with Virtual Tables

Hello everyone,

I'm seeking advice on how to generate a cumulative column within the query model, - dealing with a virtual rather than a physical table.

 

For instance, I encounter an error when attempting to use the following formula. I've marked my desired outcome in red in the attached example, but I'm uncertain about the correct approach to achieve this result. Any guidance would be greatly appreciated.

 

CupidC_0-1708324527533.png

 

3 ACCEPTED SOLUTIONS
amitchandak
Super User
Super User

@CupidC ,

Try a new column

Sumx(filter(Table, [Value] <= Earlier([Value]) ), [Value])

View solution in original post

Hi Amitchandak,

 

Thank you! It's working perfectly. I have another question: is there a possibility to use a variable instead of using  Earlier? I've come across several posts recommending against this function and was wondering if there's an alternative method involving variables or something else..  thanks!

 

Thanks

Cupid

View solution in original post

Hi @CupidC ,
You can try this

DemoTable = 
VAR vTable =
    DATATABLE (
        "Index",INTEGER,
        "Name", STRING,
        "Value", INTEGER,
        {
            { 1,"A", 1 },
            { 2,"A", 2 },
            { 3,"A", 3 }
        }
    )
VAR vAddRT =
    ADDCOLUMNS (
        vTable,
        "RT",
            VAR thisrowindex = [Index]
            RETURN
                SUMX ( FILTER ( vTable, [Index] <= thisrowindex ), [Value] )
    )
RETURN
    vAddRT

Final output

vheqmsft_0-1708498236537.png

Best regards,

Albert He

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

View solution in original post

5 REPLIES 5
v-heq-msft
Community Support
Community Support

Hi @CupidC ,
Here is a dax expression that does not use the earlier function:

Accumulation = 
CALCULATE(
    SUM('Table'[VALUE]),
    FILTER(
        ALLEXCEPT('Table','Table'[ID]),
        'Table'[VALUE] <= MAX('Table'[VALUE])
        )
    )

Final output

vheqmsft_0-1708410522197.png

For more information about the operation of accumulation you can refer to this blog
How to Calculate Cumulative Values with DAX - Microsoft Fabric Community

 

Best regards,

Albert He

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

Hi Albet

 

Thanks,, but the table is created virtually, so i can't reference "Table" in query mode, do you have other ways to achieve the result..?

Hi @CupidC ,
You can try this

DemoTable = 
VAR vTable =
    DATATABLE (
        "Index",INTEGER,
        "Name", STRING,
        "Value", INTEGER,
        {
            { 1,"A", 1 },
            { 2,"A", 2 },
            { 3,"A", 3 }
        }
    )
VAR vAddRT =
    ADDCOLUMNS (
        vTable,
        "RT",
            VAR thisrowindex = [Index]
            RETURN
                SUMX ( FILTER ( vTable, [Index] <= thisrowindex ), [Value] )
    )
RETURN
    vAddRT

Final output

vheqmsft_0-1708498236537.png

Best regards,

Albert He

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

amitchandak
Super User
Super User

@CupidC ,

Try a new column

Sumx(filter(Table, [Value] <= Earlier([Value]) ), [Value])

Hi Amitchandak,

 

Thank you! It's working perfectly. I have another question: is there a possibility to use a variable instead of using  Earlier? I've come across several posts recommending against this function and was wondering if there's an alternative method involving variables or something else..  thanks!

 

Thanks

Cupid

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.