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

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"

Hi,

 

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

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
Top Kudoed Authors