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

Calculate the most common cost

 

Greetings,

please help to write a measure that calculates the most common cost for each product.

 

Most commom cost.jpg

1 ACCEPTED SOLUTION
erik_tarnvik
Solution Specialist
Solution Specialist

Try this:

 

First a measure to count rows.

 

 

NoRows = COUNTROWS(Data)

Then figure out the most common cost. The FIRSTNONBLANK deals with ties, without it you will get an error in the precense of the two (or more) cost values being most common.

 

 

 

MostCommonCost = 
FIRSTNONBLANK(
    TOPN(
        1, 
        VALUES(Data[Cost]), 
        RANKX(ALL(Data[Cost]),[NoRows],,ASC)
    ), 
    1
)

That should do it.

 

View solution in original post

9 REPLIES 9
erik_tarnvik
Solution Specialist
Solution Specialist

Try this:

 

First a measure to count rows.

 

 

NoRows = COUNTROWS(Data)

Then figure out the most common cost. The FIRSTNONBLANK deals with ties, without it you will get an error in the precense of the two (or more) cost values being most common.

 

 

 

MostCommonCost = 
FIRSTNONBLANK(
    TOPN(
        1, 
        VALUES(Data[Cost]), 
        RANKX(ALL(Data[Cost]),[NoRows],,ASC)
    ), 
    1
)

That should do it.

 

Sorry, but when I introduce the code, the equivalent for [NoRows] gets underlained in red and tells me that "Argument '3' in ALL function is required'. Which could be the error here?:

 

 

1. M.PST = 
FIRSTNONBLANK(
    TOPN(
        1;
        VALUES('Datos Numericos'[1.PST Compromiso]);
        RANKX(ALL('Datos Numericos'[1.PST Compromiso];[Nº Filas];;ASC)
     );
     1        
 )

 

Where [Nº Filas] = COUNTROWS('Datos Numericos')

 

 

@GuillemXII, you missed a closing parenthesis:
ALL('Datos Numericos'[1.PST Compromiso]

A silly mistake on my part... It works fine now! Thank you!

@GuillemXII, you missed a closing parenthesis:
ALL('Datos Numericos'[1.PST Compromiso]

@erik_tarnvik, great thanks. That helped me a lot.

Zubair_Muhammad
Community Champion
Community Champion

Hi @dmytro_poUse this measure

CommonCost :=
CALCULATE (
    VALUES ( Table1[Cost] ),
    FILTER (
        Table1,
        MAXX ( Table1, CALCULATE ( COUNT ( Table1[Cost] ) ) )
            = CALCULATE ( COUNT ( Table1[Cost] ) )
    )
)

Regards
Zubair

Please try my custom visuals
dmytro_po
Frequent Visitor

Greetings!

Could you please help to creare a measure, which would calculate the most comon cost for each product.

Thank you in advance for your help.

 

Most commom cost.jpg

 

One way to do this:

 

For this Enter Data query:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCijKTylNLlEwVNJRMlWK1UEVMcMQMSdZxAgoYkKWiCmGiCGKiDFWESMMEWMMEVMi1GAViQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Product = _t, Cost = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product", type text}, {"Cost", Int64.Type}})
in
    #"Changed Type"

You can create this measure:

 

Mode = IF(
   CALCULATE(
       MAXX(
          VALUES(ProductCosts[Cost]),
              CALCULATE(
                    COUNTROWS(ProductCosts)
              )
       ),
       ALLSELECTED(ProductCosts)
   )
   = COUNTROWS(ProductCosts),
   "MODE",
   BLANK()
 )

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