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
Jan_Trummel
Helper III
Helper III

Previous Amount: Total row is not correct

Hello community,

 

today I have a difficult question for you and I hope someone ca help me.

 

My Data

To get started, I want to show you my tables:

The table Orders

Jan_Trummel_0-1651733600989.png

This is the fact-table. You can see, that for Product ID 2, there is no amount in February and March (keep that in mind, please).

 

The table Products

Jan_Trummel_1-1651733712580.png

This is the dimension-table. You can see 3 products here.

 

The table Calendar

And I have a date-dimension, which stores a date period from January to April 2022.

 

The data modell

Here is my data modell. It's very simple:

Jan_Trummel_2-1651733936178.png

Of course, Products and Orders are connected via columns Product ID and Product ID

Calendar and Orders are connected via Date and Order Date.

 

The task

When I show my data in a matrix, I get this report:

Jan_Trummel_3-1651734385259.png

Like I said, there is no amount for Product B in February and March.

So now, here is the task:

If I don't have an amount in a month, I want to show the last amount.

 

The measure Previous Amount

In this video from Alberto Ferrari, I found a possible solution to get the last amount:

Optimizing LASTNONBLANK and LASTNONBLANKVALUE calculations 

 

Here is my measure Previous Amount:

 

Previous Amount = 
VAR LastVisibleDate = MAX('Calendar'[Date])
VAR PreviousOrderDate = 
    CALCULATETABLE(
        LASTNONBLANK('Calendar'[Date], [Amount]),
        'Calendar'[Date] < LastVisibleDate
    )
VAR Result = CALCULATE([Amount], PreviousOrderDate)
RETURN
Result

 

 

 

 

The measure Amount is this:

 

Amount = SUM(Orders[Amount])

 

 

When I put Previous Amount in the matrix, I get this report:

Jan_Trummel_4-1651735542214.png

In February and March, the measure now shows the right amount for product B.

But the Total row isn't correct (look at the values circled in red).

 

Of course, in the context of the Total row, there is no product B in the months February and March, so the results are:

Total February = 900 + 700 = 1,600

Total March = 700 + 100 = 800

 

So, now here is the problem:

How can I write code, that calculates the right result:

Total February = 900 + 950 + 700 = 2,550

Total March = 700 + 950 + 100 = 1,750

 

I just tried it with SUMX and SUMMARIZE, but it doesn't work.

Do you have an idea?

 

Thanks a lot and have a nice day!

Greetings

1 ACCEPTED SOLUTION

Hi @Jan_Trummel ,

 

I create a sample by the data you provided above. I think you can try this measure to achieve your goal.

M_Amount = SUM(Orders[Amount])
Previous Amount = 
VAR _GENERATE =
    GENERATE ( VALUES ( Products[Product Name] ), VALUES ( 'Calendar'[Month Nr] ) )
VAR _ADD =
    ADDCOLUMNS (
        _GENERATE,
        "Previous Amount",
            VAR _LastVisibleDate =
                CALCULATE ( MAX ( 'Calendar'[Date] ) )
            VAR _PreviousOrderDate =
                CALCULATETABLE (
                    LASTNONBLANK ( 'Calendar'[Date], [M_Amount] ),
                    'Calendar'[Date] < _LastVisibleDate
                )
            VAR _Result =
                CALCULATE ( [M_Amount], _PreviousOrderDate )
            RETURN
                _Result
    )
RETURN
    SUMX ( _ADD, [Previous Amount] )

Result is as below.

RicoZhou_1-1652175342456.png

 

Best Regards,
Rico Zhou

 

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

4 REPLIES 4
Jan_Trummel
Helper III
Helper III

Hello @amitchandak ,

thank you for your answer. Unfortunatelly, that's not the right code.

Your measure returns a date value:

Jan_Trummel_0-1651754859049.png

Do you have another idea?

Hi @Jan_Trummel ,

 

I create a sample by the data you provided above. I think you can try this measure to achieve your goal.

M_Amount = SUM(Orders[Amount])
Previous Amount = 
VAR _GENERATE =
    GENERATE ( VALUES ( Products[Product Name] ), VALUES ( 'Calendar'[Month Nr] ) )
VAR _ADD =
    ADDCOLUMNS (
        _GENERATE,
        "Previous Amount",
            VAR _LastVisibleDate =
                CALCULATE ( MAX ( 'Calendar'[Date] ) )
            VAR _PreviousOrderDate =
                CALCULATETABLE (
                    LASTNONBLANK ( 'Calendar'[Date], [M_Amount] ),
                    'Calendar'[Date] < _LastVisibleDate
                )
            VAR _Result =
                CALCULATE ( [M_Amount], _PreviousOrderDate )
            RETURN
                _Result
    )
RETURN
    SUMX ( _ADD, [Previous Amount] )

Result is as below.

RicoZhou_1-1652175342456.png

 

Best Regards,
Rico Zhou

 

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

Hello @v-rzhou-msft ,

 

this is exactly what I was looking for! Thank you very much!

 

Greeting!

amitchandak
Super User
Super User

@Jan_Trummel , try like

 

Previous Amount =
VAR LastVisibleDate = MAX('Calendar'[Date])
VAR Result = CALCULATE(LASTNONBLANK('Calendar'[Date], [Amount]), filter(all('Calendar'[Date]) ,'Calendar'[Date] < LastVisibleDate))
RETURN
Result

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.