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
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
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.

Top Solution Authors