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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

Calculate running totals and assign a default category value based on max Trans date

Hi guys ,

 

This is my first post on here , so i hope i can explain my issue the best way i can .

 

Sample Data Set:

inventory Table

okubizzy_0-1639437780200.png

 

S/NItemid AmountCategoryPostingTypeTransdate
110000 $    25.00    A11/1/2020
210001 $    25.00    B11/15/2020
310000 $    25.00    B11/15/2020
410001 $    25.00    A21/31/2020
510000 $    25.00    C21/31/2020
610000 $    25.00    B12/1/2020
710001 $    25.00    A12/15/2020
810001 $    25.00    C12/15/2020
910000 $    25.00    A12/26/2020
1010000 $    25.00    D22/28/2020
1110001 $    25.00    C22/28/2020

I am trying to assign the correct 'Category' for  an item (every month)  based on the following conditions ;

1) Transdate is month end 

2)  Posting Type value  = 2


Based on the conditions above. If the logic is correct, I should have the result in green below:

okubizzy_6-1639427533537.png

I want to be able to dynamically select any month in a slicer and get the result above;

 

i have been able to calculate running totals  using the dax below , however calulating the category (in a measure)  has been difficult .However when i introduce the category field , i get the wrong  value above .

 

i am wondering if there is way to recalculate the category field to get the desired result 

 

TotalValue:= SUM('table'[Amount])


CurrentValueRT:=

VAR MAXDATE = MAX('table'[TransDate])
VAR RESULT =
CALCULATE( 'table'[TotalValue],

'table'[TransDate]<= MAXDATE,
ALL ('table'[TransDate])
)
RETURN
RESULT

 

 

 

Hopefully this makes sense and any help much appreciated.  i am not an expert and was trying to use a calculated column but i believe a measure might work better 

 

Thanks in advance 

 

 

Regards,

Ola

 

 

 

 

 

 

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

I was able to resolve this using the dax below:

Total Amount =
VAR __cat = MAX( 'Table'[Category] )
VAR __summary =
CALCULATETABLE(
ADDCOLUMNS(
ADDCOLUMNS(
SUMMARIZE(
'Table',
'Table'[ITEMID],
'Table'[WarehouseID]
),
"@amt", CALCULATE( SUM( 'Table'[Amount] ) ),
"@maxdate", CALCULATE( MAX( 'Table'[TRANSDATE] ) )
),
"@Cat",
VAR __dt = [@maxdate]
RETURN
CALCULATE(
MAX( 'Table'[Category] ),
'Table'[TRANSDATE] = __dt
)
),
ALLSELECTED( 'Table'[Category] )
)
RETURN
IF(
__cat IN SELECTCOLUMNS( __summary, "_cat", [@Cat] ),
SUMX( FILTER( __summary, [@Cat] = __cat ), [@amt] )
)

View solution in original post

30 REPLIES 30
Anonymous
Not applicable

I was able to resolve this using the dax below:

Total Amount =
VAR __cat = MAX( 'Table'[Category] )
VAR __summary =
CALCULATETABLE(
ADDCOLUMNS(
ADDCOLUMNS(
SUMMARIZE(
'Table',
'Table'[ITEMID],
'Table'[WarehouseID]
),
"@amt", CALCULATE( SUM( 'Table'[Amount] ) ),
"@maxdate", CALCULATE( MAX( 'Table'[TRANSDATE] ) )
),
"@Cat",
VAR __dt = [@maxdate]
RETURN
CALCULATE(
MAX( 'Table'[Category] ),
'Table'[TRANSDATE] = __dt
)
),
ALLSELECTED( 'Table'[Category] )
)
RETURN
IF(
__cat IN SELECTCOLUMNS( __summary, "_cat", [@Cat] ),
SUMX( FILTER( __summary, [@Cat] = __cat ), [@amt] )
)
Anonymous
Not applicable

This measure works very well  when calculating the cummulative totals ,

However the cummulative totals are calculated at monthly levels not at daily levels. (any ideas on how this can be fixed)

 

Totalvalue2 =
SUMX (
FILTER (
ALL ( ' SAMPLE DATA' ),
[Itemid] IN DISTINCT ( 'SAMPLE DATA'[Itemid] )
&& [WarehouseID] IN DISTINCT ( 'SAMPLE DATA'[WarehouseID] )
&& [Transdate] <= SELECTEDVALUE ( 'SAMPLE DATA'[Transdate] )
),
[Amount]
)

@Anonymous 

 

vjaneygmsft_0-1640681819711.png

You need to modify depending on your context.

Anonymous
Not applicable

Hi Thanks for this however i couldnt solve the answer based on the query above .

 

thanks again

@Anonymous 

 

For the new things you mentioned later, I can't see where your questions and needs are, and I can't see the needs from your files. And It has almost nothing to do with your original problem. I asked you but didn't say anything. How can I help you?

Anonymous
Not applicable

Hi @v-janeyg-msft , 

 

thanks for your response ,  the original question was based off a logic & sample dataset  i tried to implement which didnt work .

 

you requested for a wider data set which reflects the real problem which i have created 

 

i will put the question below again

Below is a drop box link with a sample dataset :

https://www.dropbox.com/scl/fi/8cqz36yc44brmf2rzre7w/MATDW-SAMPLE-DATA_v2.xlsx?dl=0&rlkey=jzgswrfhet...


I will explain my challenge from scratch to help you understand better:


The Summary of my challenges is :


The (Running total) "Total Amount" for an item should be assigned to the most recent category based on the Transdate (as filtered by the slicer) .

 

 

The Total amount (Business Definition):

The sum of All prior sales of an item to date ( Transdate selected in slicer) .

 


The Category of an item (Business Definition) :

The category of the item should be the most recent category based on the transdate (as filtered by the slicer)

 

 

i have also attached a Sample answer calculated from the Excel sheet

I am also looking to get (Totals by category) "calculated categories above" for every warehouse :

NB: Totals might not be correct.

okubizzy_0-1640068857550.png

 

@Anonymous 

 

Sorry, due to the nature of work, we can't reply to anything outside of the post.
I asked questions because you didn't provide anything that you operate in powerbi, but just describe your needs. The forum will give you help in the process of making the pbi report, but if you want me to help you make a complete report, it’s a bit difficult.

 

So the best way is to share a sample pbix file of yours, and then describe the problems you encountered in it.

If it doesn't work, you still need to answer my question first, and then I will help you based on my understanding, but it is difficult to get it right in one step.

Reference:How to Get Your Question Answered Quickly - Microsoft Power BI Community

 

Best Regards,
Community Support Team _ Janey

 

Anonymous
Not applicable

Thanks for your help :

 

i have been able to calculate  the right categories & totals by tweaking the dax queries as show  below : 

 
this works well  as shown below ;
okubizzy_2-1640077398104.png

 

Measure =
VAR a =
MAXX (
FILTER (
ALL ( 'SAMPLE DATA' ),
MONTH ( [Transdate] ) = MONTH ( SELECTEDVALUE ( 'SAMPLE DATA'[Transdate] ) )
),
[Transdate]
)
RETURN
IF (
SELECTEDVALUE ( 'SAMPLE DATA'[Transdate] ) = a
,
1,
0
)
---------
Totalvalue2 =
SUMX (
FILTER (
ALL ( ' SAMPLE DATA' ),
[Itemid] = SELECTEDVALUE ( 'SAMPLE DATA'[Itemid] )
/* && [Category] = SELECTEDVALUE ( 'SAMPLE DATA'[Category] ) */
&&[WarehouseID]= SELECTEDVALUE ( 'SAMPLE DATA'[WarehouseID] )
&& MONTH ( [Transdate] ) <= MONTH ( SELECTEDVALUE ( 'SAMPLE DATA'[Transdate] ) )
&& YEAR ( [Transdate] ) <= YEAR ( SELECTEDVALUE ( 'SAMPLE DATA'[Transdate] ) )
&& [Transdate] <= SELECTEDVALUE ( 'SAMPLE DATA'[Transdate] )
),
[Amount]
)
 
However there are two main  issues :
 
1.)when i select an older transdate, the  totalvalue & categories become blank
okubizzy_1-1640077325176.png

2) when i try to total value by calculated  category , if an item is not selected the "calculated category groupings " dont filter the table 

 

 
 
 
 
 
 
 

Hi, @Anonymous 

 

vjaneygmsft_1-1640079501723.png

I originally wrote this measure to filter row data based on your original question, but according to your current visual, this measure has no meaning at all...and it will cause errors after you use the date slicer, such as not displaying the data.   You don't need to put this measure in filter pane.

 

vjaneygmsft_2-1640079922029.png

The logic here is a bit messy, can you describe the date range in text? Let me see if there is any problem.

 

Anonymous
Not applicable

the date range is july 2009  till date

@Anonymous  According to your logic, You only need to use:

[Transdate] <= SELECTEDVALUE ( 'SAMPLE DATA'[Transdate] )&&[Transdate]>=date(2009,7,1)

And  ALL ('SAMPLE DATA') is used above, if july 2009 is the smallest date in your data source. One condition is enough.

Like this:

Totalvalue2 =
SUMX (
    FILTER (
        ALL ( ' SAMPLE DATA' ),
        [Itemid] = SELECTEDVALUE ( 'SAMPLE DATA'[Itemid] ) /* && [Category] = SELECTEDVALUE ( 'SAMPLE DATA'[Category] ) */
            && [WarehouseID] = SELECTEDVALUE ( 'SAMPLE DATA'[WarehouseID] )
            && [Transdate] <= SELECTEDVALUE ( 'SAMPLE DATA'[Transdate] )
    ),
    [Amount]
)

Did I answer your question ? Please mark my reply as solution. Thank you very much.
If not, please feel free to ask me.

 

Best Regards,
Community Support Team _ Janey

Anonymous
Not applicable

Thanks a lot , i must admit this has been a long road and i appreciate the effort in helping overcome the challenge .

 

The measure works at  getting individual item categories (calculated) and totals .(Item level) .

it is  dynamic based on the transdate  which is great news.

 

However i am also trying to return  totals by categories (Calculated categories) based on transdate selected.(summary data )

 when i select an item a single item  , it returns a value ( works properly)

 

okubizzy_4-1640150666522.png

 

 

 

 

Whenever i select "ALL" / multiple selections  it returns blank / wrong values for the totals and categories as shown below:

okubizzy_0-1640150425884.png

 

 

okubizzy_5-1640150709019.png

 

 

okubizzy_0-1640152244045.png

 

i would like to get values when i select  "ALL " & "Multiple selections" 

 

thanks,

Okubizzy

 

 

@Anonymous 

 

Modify it using dictinct instead of selectedvalue:

Totalvalue2 =
SUMX (
    FILTER (
        ALL ( ' SAMPLE DATA' ),
        [Itemid]
            IN DISTINCT ( 'SAMPLE DATA'[Itemid] ) /* && [Category]  in distinct ( 'SAMPLE DATA'[Category] ) */
                && [WarehouseID]
                    IN DISTINCT ( 'SAMPLE DATA'[WarehouseID] )
                        && [Transdate] <= SELECTEDVALUE ( 'SAMPLE DATA'[Transdate] )
    ),
    [Amount]
)

 DISTINCT (column) function (DAX) - DAX | Microsoft Docs

 

Best Regards,
Community Support Team _ Janey

Anonymous
Not applicable

This  is great work. 

This measure  works as expected when calculating totals  at end of month dates .

 

for instance : when selecting a transdate that is monthend ( 2021-10-31)  it returns cummlative totals .

 

however when a  transdate date that is not month end  date is selected ( 2021-10-13)  the measure performs unexpectedly.

 

it dosent return cummulative totals.

 

i am trying to get it working for any transdate  date selected in range .

@Anonymous 

Because there is no restriction on the date column in the measure, it just calculates the total amount that is less than this date based on the date column you choose.

 

If you want to calculate dates within a certain range, you can also modify the measure.

You need to change the date slicer into a relative date range and modify like this:

vjaneygmsft_0-1640156483033.png

 

[Transdate] in distinct ( 'SAMPLE DATA'[Transdate] )

 

Best Regards,
Community Support Team _ Janey

 

Anonymous
Not applicable

This changes the result set (totals) as i am not trying to use a particular range.

 

I am just trying to calculate cummulative totals for any trans dates selected  in the slicer.

however the current measure only returns accurately when date selected is an End of month date

 

 

@Anonymous  

 

What's your measure?

Anonymous
Not applicable

Totalamount =
SUMX (
FILTER (
ALL ( 'Table' ),
[Itemid] in DISTINCT ( 'Table'[Itemid] )
&&[WarehouseID] in DISTINCT ( 'Table'[WarehouseID] )
&& [TransDate] <= SELECTEDVALUE ( 'Table'[TransDate] )
),
[Amount]
)

 

That is my measure for calculating cummulative totals .

 

also would it be possible  to calculate ytd values based on this logic?

 

 

 

@Anonymous 

 

vjaneygmsft_0-1640159550824.png

There is no problem with the logic of the measure. Since you said that the result is wrong, Can you share the difference between the wrong result and the correct result? In this way, I can make a comparison to judge where I misunderstood.

vjaneygmsft_2-1640159819407.png

 

 

vjaneygmsft_1-1640159680499.png

It's ok. Depends on your needs.

Anonymous
Not applicable

wrong result :

 

When Trans date selected is not an end of month date as shown below  

 

okubizzy_0-1640160841771.png

Correct result when end of month date is selected as transdate as shown below

 

end of month value :

 

okubizzy_1-1640160941789.png

Thanks .

 

 

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.