cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
zileng
Frequent Visitor

Sum of value with Different Scenarios

Hi,

 

I need to calculate sum of value with these scenarios:

 

  • If there's > 1 version, take the latest version
  • If there's > 1 options, take Option 1.

 

ClassVersionOptionsValue
A1NO1,000.00
A2NO2,000.00
B1Option 11,050.00
B1Option 21,100.00
C1NO500.00
C2NO600.00
C3NO700.00

 

Sum = 2,000 + 1,050 + 700 = 3,750

 

Thank you.

 

Regards,

Zi

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

Hi @zileng ,

 

We can create a measure to meet your requirement:

 

Total Value =
SUMX (
    SUMMARIZE (
        'Table',
        'Table'[Class],
        "Sum",
        VAR maxVersion =
            CALCULATE ( MAX ( 'Table'[Version] ) )
        RETURN
            IF (
                CALCULATE (
                    DISTINCTCOUNT ( 'Table'[Options] ),
                    FILTER ( 'Table', 'Table'[Version] = maxVersion )
                ) = 1,
                CALCULATE (
                    SUM ( 'Table'[Value] ),
                    FILTER ( 'Table', 'Table'[Version] = maxVersion )
                ),
                CALCULATE (
                    SUM ( 'Table'[Value] ),
                    FILTER (
                        'Table',
                        'Table'[Version] = maxVersion
                            && 'Table'[Options] = "Option 1"
                    )
                )
            )
    ),
    [Sum]
)

 

6.jpg


Best regards,

Community Support Team _ Dong Li
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

3 REPLIES 3
v-lid-msft
Community Support
Community Support

Hi @zileng ,

 

We can create a measure to meet your requirement:

 

Total Value =
SUMX (
    SUMMARIZE (
        'Table',
        'Table'[Class],
        "Sum",
        VAR maxVersion =
            CALCULATE ( MAX ( 'Table'[Version] ) )
        RETURN
            IF (
                CALCULATE (
                    DISTINCTCOUNT ( 'Table'[Options] ),
                    FILTER ( 'Table', 'Table'[Version] = maxVersion )
                ) = 1,
                CALCULATE (
                    SUM ( 'Table'[Value] ),
                    FILTER ( 'Table', 'Table'[Version] = maxVersion )
                ),
                CALCULATE (
                    SUM ( 'Table'[Value] ),
                    FILTER (
                        'Table',
                        'Table'[Version] = maxVersion
                            && 'Table'[Options] = "Option 1"
                    )
                )
            )
    ),
    [Sum]
)

 

6.jpg


Best regards,

Community Support Team _ Dong Li
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

mcybulski
Solution Specialist
Solution Specialist

The code below presumes that you data is sorted in the manner by version and then option.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIEYj9/EEPHwMBAz8BAKVYHImMEkzFClnGC6vEvKMnMz1MwhOg0xSVvBJY3ROh3RrbTFFUcbqMZqrgxTNwcKh4LAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Class = _t, Version = _t, Options = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Class", type text}, {"Version", Int64.Type}, {"Options", type text}, {"Value", Int64.Type}}),
    AddFirst = Table.AddColumn(#"Changed Type", "First", each if [Options] <> "NO" then [Value] else null),
    AddLast = Table.AddColumn(AddFirst, "Last", each if [Options] = "NO" then [Value] else null),
    #"Grouped Rows" = Table.Group(AddLast, {"Class"}, {{"First", each List.First([First]), type number}, {"Last", each List.Last([Last]), type number}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Value", each List.First(List.RemoveNulls({[First],[Last]}))),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"First", "Last"})
in
    #"Removed Columns"
zileng
Frequent Visitor

Hi,

 

I am new to this. I don't understand the codes.

Helpful resources

Announcements
Power BI December 2021 Update_carousel 768x460.jpg

Check it Out!

Click here to read more about the December 2021 Updates!

Jan 2022 Dev Camp 768x460 copy.png

Power BI Dev Camp- January 27th, 2022

Mark your calendars and join us for our next Power BI Dev Camp!

UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Top Solution Authors