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
LiborK
Frequent Visitor

Running Total

HHello, I don't know, how to write in dax Running Total Query for below situation:

LiborK_0-1652528635250.png

I only find examples for situations where the Count ID is not similar in one Column. So I get values in Column Actual Running Total. Do you know how to write it?

 

Thank you in advance.

 

2 ACCEPTED SOLUTIONS
SpartaBI
Community Champion
Community Champion

@LiborK 
So, no calculated columns. Create this measure:

 

Count RT = 
VAR _current_count = [COUNT ID'S]
VAR _current_id = SELECTEDVALUE('Table'[ID])
VAR _max_customer_id = 
    ROUND(CALCULATE(MAX('Table'[ID]), ALLSELECTED('Table'[ID]),-1)
VAR _ranking_table = 
    ADDCOLUMNS(
        ALLSELECTED('Table'[ID]),
        "@%ID", [% ID],
        "@Ranking", [COUNT ID'S] * _max_customer_id + 'Table'[ID]
    ) 
VAR _current_rank =  _current_sales * _max_customer_id + _current_id
VAR _result = 
    SUMX(
        FILTER(
            _ranking_table,
            [@Ranking] >= _current_rank
        ),
        [@%ID]
    ) 
RETURN 
    _result

 


2022-05-09 22_36_04-Power BI Workspace Datasets License Permissions - Microsoft Power BI Community.png

Showcase Report – Contoso By SpartaBI


SpartaBI_3-1652115470761.png  SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Full-Logo11.png

View solution in original post

v-cazheng-msft
Community Support
Community Support

Hi @LiborK,

 

You may try this solution.

Here are the Measures created.

 

Count ID = CALCULATE(COUNT('Table'[ID]),ALLEXCEPT('Table','Table'[ID]))

Total Count of IDs = CALCULATE(COUNT('Table'[ID]),ALL('Table'))

%ID = DIVIDE([Count ID],'Table'[Total Count of IDs])

Wished Running Sum = CALCULATE([%ID],FILTER(ALL('Table'),'Table'[RANK]>=MAX('Table'[RANK])))

 

 

Here are the Calculated columns created.

 

DistinctCountID =CALCULATE(COUNT('Table'[ID]),FILTER(ALL('Table'),'Table'[ID]=EARLIER('Table'[ID])))

RANK =
VAR MaxID =
    CALCULATE ( MAX ( 'Table'[ID] ), ALLEXCEPT ( 'Table', 'Table'[DistinctCountID] ) )
VAR Addition =
    ROUND ( 'Table'[%ID], 5 ) * 100000
RETURN
    IF ( 'Table'[ID] < MaxID, Addition + MaxID, Addition )

 

 

Then, the result look like this.

vcazhengmsft_0-1652781093128.png

 

Also, attach the pbix file as reference.

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let me know. Thanks a lot!

 

Best Regards,

Community Support Team _ Caiyun

View solution in original post

13 REPLIES 13
v-cazheng-msft
Community Support
Community Support

Hi @LiborK,

 

You may try this solution.

Here are the Measures created.

 

Count ID = CALCULATE(COUNT('Table'[ID]),ALLEXCEPT('Table','Table'[ID]))

Total Count of IDs = CALCULATE(COUNT('Table'[ID]),ALL('Table'))

%ID = DIVIDE([Count ID],'Table'[Total Count of IDs])

Wished Running Sum = CALCULATE([%ID],FILTER(ALL('Table'),'Table'[RANK]>=MAX('Table'[RANK])))

 

 

Here are the Calculated columns created.

 

DistinctCountID =CALCULATE(COUNT('Table'[ID]),FILTER(ALL('Table'),'Table'[ID]=EARLIER('Table'[ID])))

RANK =
VAR MaxID =
    CALCULATE ( MAX ( 'Table'[ID] ), ALLEXCEPT ( 'Table', 'Table'[DistinctCountID] ) )
VAR Addition =
    ROUND ( 'Table'[%ID], 5 ) * 100000
RETURN
    IF ( 'Table'[ID] < MaxID, Addition + MaxID, Addition )

 

 

Then, the result look like this.

vcazhengmsft_0-1652781093128.png

 

Also, attach the pbix file as reference.

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let me know. Thanks a lot!

 

Best Regards,

Community Support Team _ Caiyun

It works thank you so much!

SpartaBI
Community Champion
Community Champion

@LiborK yes, but you need to choose which one is your 2nd important column for the sorting (the 1st is of course the [Count ID] )
Let's say it's the [ID] column and let's say higher ID -> Higher rank
So, first, create this calculated column:

 

 

'Table'[Sorting] = 'Table'[Count ID] * ROUND(MAX('Table'[ID]), -1) + 'Table'[ID].

 

 


Then create this Count RT column:

 

 

'Table'[Count RT] =
VAR _current_rank = 'Table'[Sorting]
RETURN
SUMX(FILTER('Table, 'Table'[Sorting] <= _current_rank), 'Table'[% ID])

 

 


2022-05-09 22_36_04-Power BI Workspace Datasets License Permissions - Microsoft Power BI Community.png

Showcase Report – Contoso By SpartaBI


SpartaBI_3-1652115470761.png  SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Full-Logo11.png

Hi, thank you for you answer, but I am not able to choose 

 

'Table'[Sorting] = 'Table'[Count ID] * ROUND(MAX('Table'[ID]), -1) + 'Table'[ID].

 

'Table' [ID] in second step after "+" it says Cannot find name 'ID'.

If its helps Count IDs, Total Count Of IDs, %ID, Actual Running Sum are measures...

SpartaBI
Community Champion
Community Champion

@LiborK you have that column in your model? 'Table'[ID]?

Also, the picture you sent is a matrix visual where you put that column on the rows of a matrix and all the rest are measures? Is all coming from the same table or there are several tables involved? Best if you can share the file but maybe will be able to answer without, just with answers to my questions.

Yes, the column is in my model...I am able to find it for the green colored part, but not for the red colored part:

'Table'[Sorting] = 'Table'[Count ID] * ROUND(MAX('Table'[ID]), -1) + 'Table'[ID], only column which is not measure is "ID" from the example above, but it is distinct like in pivot table. Original table looks like this example: 

LiborK_0-1652690580044.png

 

 

SpartaBI
Community Champion
Community Champion

@LiborK 
So, no calculated columns. Create this measure:

 

Count RT = 
VAR _current_count = [COUNT ID'S]
VAR _current_id = SELECTEDVALUE('Table'[ID])
VAR _max_customer_id = 
    ROUND(CALCULATE(MAX('Table'[ID]), ALLSELECTED('Table'[ID]),-1)
VAR _ranking_table = 
    ADDCOLUMNS(
        ALLSELECTED('Table'[ID]),
        "@%ID", [% ID],
        "@Ranking", [COUNT ID'S] * _max_customer_id + 'Table'[ID]
    ) 
VAR _current_rank =  _current_sales * _max_customer_id + _current_id
VAR _result = 
    SUMX(
        FILTER(
            _ranking_table,
            [@Ranking] >= _current_rank
        ),
        [@%ID]
    ) 
RETURN 
    _result

 


2022-05-09 22_36_04-Power BI Workspace Datasets License Permissions - Microsoft Power BI Community.png

Showcase Report – Contoso By SpartaBI


SpartaBI_3-1652115470761.png  SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Full-Logo11.png

Thank you so much for your help, but it is still not working...I will try to describe it in different way.

I have just table with this column "ID" and values in this column are with structure like in picture below ( for example 13, can be there multiple times) and I dont have there any other measures.

LiborK_0-1652698568152.png

 

And my task is to create table where in first column will be ID (for each row unique ID) and the rest of the column will be like in picture 2 

LiborK_1-1652699782345.png

and I am able to create measures for column B-E, but I am not able to create measure for column F.

VAR _ranking_table =
ADDCOLUMNS(
ALLSELECTED('Table'[ID]),
"@%ID"(parametr is not in correct type), [% ID](I am not able to find the measure, it provide me just standard column from model),
"@Ranking", [COUNT ID'S] * _max_customer_id + 'Table'[ID]
)

SpartaBI
Community Champion
Community Champion

@LiborK are you trying to create a matrix visual or a calculated table?

Table 

LiborK_0-1652701431206.png

 

SpartaBI
Community Champion
Community Champion

  • @LiborK ok, then my measure should work. Show me the fields you put on the buckets and also the result of them and of my measure. If you get an error in my measure, please verify that all the column names and measures are correct

Also, look at my code @ID is a temporary column I created. I'm adding @ to distinguish it from regular model columns

tamerj1
Super User
Super User

Hi @LiborK 

would you please clarify further?

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