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
Anonymous
Not applicable

Calculate median

Hi,

 

I have a dataset.

Config IDIs similarpart gp descitemTotal ProfitOutput Column -Median
ABC-12345Yesgp1item1870769
ABC-12345Yesgp2item2947769
ABC-12345Yesgp3item3844769
ABC-12345Yesgp4item4844769
ABC-12345Nogp5item5769769
ABC-12345Nogp6item6146769
ABC-12345Nogp7item7954769
ABC-12345Nogp8item8455769
ABC-12345Nogp9item9455769
XYZ-12345Yesgp10item10560888
XYZ-12345Yesgp11item11814888
XYZ-12345Yesgp12item12885888
XYZ-12345Yesgp13item13717888
XYZ-12345Nogp14item14888888
XYZ-12345Nogp15item15487888
XYZ-12345Nogp16item16152888
XYZ-12345Nogp17item17881888
XYZ-12345Nogp18item18881888


I need to calculate median at Config level which is described in the 'output column - median'

2 issues that im facing is :

 

1. Im try to use median formula in calculate but not sure which dax to use. All,Allexcept etc

2. When i put median to the value shelf of a table in powerbi it summarizes by default. I dont want it to summarize as median is already summarized. Dont summarize option gives more than 1 row of result.

1 ACCEPTED SOLUTION
v-frfei-msft
Community Support
Community Support

Hi @Anonymous,

 

Please check the following steps as below.

 

1. We should insert an index column ranked by [Config ID] column in Power query. You can refer to the M code as below.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hdGxCsJADAbgd7m5QlOTu9yo7s7W0rGIgyjo+2NzJAWpvSz5M3wcOf5hCIfjaQfdHik0oZ/e87y9YJ73z/SQ5NTOM8Ucxua/7lRLZkyO3quWZERHo2qs6vOzYFJMyqo4KpYEjHWcFEtmcs5gxZJIVMdZcV7hS39dF9NaM7JQlMnM235pslQJ6HnrsizM5HlrsywJ0obXz4LVuRxS51ZoWZC9163SsgB1DrdS9WpwuNUK/MvHLw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Config ID" = _t, #"Is similar" = _t, #"part gp desc" = _t, item = _t, #"Total Profit" = _t, #"Output Column -Median" = _t]),
    Partition = Table.Group(Source, {"Config ID"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
    #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Is similar", "part gp desc", "item", "Total Profit", "Output Column -Median", "Index"}, {"Partition.Is similar", "Partition.part gp desc", "Partition.item", "Partition.Total Profit", "Partition.Output Column -Median", "Partition.Index"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Partition",{{"Partition.Output Column -Median", Int64.Type}, {"Partition.Total Profit", Int64.Type}})
in
    #"Changed Type"

2. Then we can create a measure to get the result as we need.

 

_Output Column -Median = var _round=ROUND(CALCULATE(COUNTROWS('table'),ALL('table'),VALUES('table'[Config ID]))/2,0)
return
CALCULATE(MAX('table'[Partition.Total Profit]),FILTER(ALLEXCEPT('table','table'[Config ID]),'table'[Partition.Index]=_round)
)

 

Capture.PNG

 

For more details, please check the pbix as attached.

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

View solution in original post

6 REPLIES 6
v-frfei-msft
Community Support
Community Support

Hi @Anonymous,

 

Please check the following steps as below.

 

1. We should insert an index column ranked by [Config ID] column in Power query. You can refer to the M code as below.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hdGxCsJADAbgd7m5QlOTu9yo7s7W0rGIgyjo+2NzJAWpvSz5M3wcOf5hCIfjaQfdHik0oZ/e87y9YJ73z/SQ5NTOM8Ucxua/7lRLZkyO3quWZERHo2qs6vOzYFJMyqo4KpYEjHWcFEtmcs5gxZJIVMdZcV7hS39dF9NaM7JQlMnM235pslQJ6HnrsizM5HlrsywJ0obXz4LVuRxS51ZoWZC9163SsgB1DrdS9WpwuNUK/MvHLw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Config ID" = _t, #"Is similar" = _t, #"part gp desc" = _t, item = _t, #"Total Profit" = _t, #"Output Column -Median" = _t]),
    Partition = Table.Group(Source, {"Config ID"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
    #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Is similar", "part gp desc", "item", "Total Profit", "Output Column -Median", "Index"}, {"Partition.Is similar", "Partition.part gp desc", "Partition.item", "Partition.Total Profit", "Partition.Output Column -Median", "Partition.Index"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Partition",{{"Partition.Output Column -Median", Int64.Type}, {"Partition.Total Profit", Int64.Type}})
in
    #"Changed Type"

2. Then we can create a measure to get the result as we need.

 

_Output Column -Median = var _round=ROUND(CALCULATE(COUNTROWS('table'),ALL('table'),VALUES('table'[Config ID]))/2,0)
return
CALCULATE(MAX('table'[Partition.Total Profit]),FILTER(ALLEXCEPT('table','table'[Config ID]),'table'[Partition.Index]=_round)
)

 

Capture.PNG

 

For more details, please check the pbix as attached.

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
Anonymous
Not applicable

HI Frank,

 

What does the m code do in the partition block ?

If you can explain me the purpose and if I can acheive the same ia UI, it would help as my actual data source is amazon redshift

Hi @Anonymous,

 

I used the step partition to insert index by catgory in power query.

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
Anonymous
Not applicable

So in my original source code do I add the same M code that I see in your pbix file. What will change?

If you could explain the structure/logic of the code that would be helpful

Hi @Anonymous,

 

Please check the document about Table.AddIndexColumn and Table.Group in Power query.

 

https://www.powerquery.io/table/table.addindexcolumn

 

https://www.powerquery.io/table/table.group

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
Greg_Deckler
Super User
Super User

Well if you really want a column versus a measure, then you *should* be able to do this:

 

Column =
MEDIANX(FILTER(ALL('Table2'),[Config ID] = EARLIER([Config ID])),[Total Profit])
 
However, I am getting an error around a variant data type returned, which should not be the case since it should return a decimal number. Seems like a bug so going to call in @marcorusso to see if that is true.

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.