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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
JacobK
Regular Visitor

Get Current or Last Non-Zero Value

I am looking to create a DAX formula that gets the sum of an amount grouped by a category and date. If the sum amount is zero for that category and date then I want to get the last non-zero value.

 

The current DAX formula that I have works great when only one single category exists, but when I add multiple categories it gets the last non-zero amount across all categories.

 

Here is the data structure (Left = "DateDim", Right = "AmountFact"):

JacobK_0-1648233786877.png

 

Very simple model:

JacobK_0-1648234536922.png

 

Here are the measures:

 

 

TotalAmount = SUM(AmountFact[Amount])

 

 

 

CurrentOrLastValue = IF( [TotalAmount] = 0,
    CALCULATE (
        [TotalAmount],
        TOPN(
            1,
            CALCULATETABLE(
                'AmountFact',
                FILTER(
                    ALL('AmountFact'),
                    [TotalAmount] > 0
                ),
                FILTER(
                    ALL( 'DateDim' ),
                    'DateDim'[Date] < max( 'DateDim'[Date] )
                )
            ),
            'AmountFact'[Date],
            DESC
        )
    ),
    [TotalAmount]
)

 

 

Here is an example of when only one category exists (Working as expected):

JacobK_1-1648234003328.png

 

Here is when multiple categories exist:

 

JacobK_1-1648234861832.png

JacobK_3-1648234144320.png

 

As you can see above the most recent non-zero value is now the most recent non-zero value of ANY category.

 

I believe that whats going on is that in the "CurrentOrLastValue" measure when I select the TOPN 1 to get the most recent non-zero record it is looking at ALL the records. I did a bunch research looking at how I could filter that CALCULATEDTABLE to only get the category of the current record without any luck. Hoping that someone can help me out here!

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @JacobK 
You may try 

CurrentOrLastValue =
VAR CurrentDate =
    MAX ( 'DateDim'[Date] )
VAR CurrentAmount = [TotalAmount]
VAR CurrentCategoryTable =
    CALCULATETABLE (
        'AmountFact',
        ALLEXCEPT ( 'AmountFact', 'AmountFact'[Category] )
    )
VAR CurrentCategoryWithValueTable =
    FILTER ( CurrentCategoryTable, [TotalAmount] > 0 )
VAR PreviousDateWithValue =
    TOPN (
        1,
        FILTER ( CurrentCategoryWithValueTable, 'DateDim'[Date] < CurrentDate ),
        'AmountFact'[Date], DESC
    )
VAR PreviousAmount =
    CALCULATE ( [TotalAmount], PreviousDateWithValue )
RETURN
    IF ( CurrentAmount = 0, PreviousAmount, CurrentAmount )

View solution in original post

1 REPLY 1
tamerj1
Super User
Super User

Hi @JacobK 
You may try 

CurrentOrLastValue =
VAR CurrentDate =
    MAX ( 'DateDim'[Date] )
VAR CurrentAmount = [TotalAmount]
VAR CurrentCategoryTable =
    CALCULATETABLE (
        'AmountFact',
        ALLEXCEPT ( 'AmountFact', 'AmountFact'[Category] )
    )
VAR CurrentCategoryWithValueTable =
    FILTER ( CurrentCategoryTable, [TotalAmount] > 0 )
VAR PreviousDateWithValue =
    TOPN (
        1,
        FILTER ( CurrentCategoryWithValueTable, 'DateDim'[Date] < CurrentDate ),
        'AmountFact'[Date], DESC
    )
VAR PreviousAmount =
    CALCULATE ( [TotalAmount], PreviousDateWithValue )
RETURN
    IF ( CurrentAmount = 0, PreviousAmount, CurrentAmount )

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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