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
aguirre_2022
Regular Visitor

Last value entered between columns(months) or by transposing last value entered from month column

HI Comumunity 

 

Its my first post , question really

 

Please your help 👍

 

I have a table with this information

{product ,january ,february, march}

a          100          12          0

b                          50        30

c            100        120      40

 

I need to get the last non-zero value

this value is called "quantity" with respect to month columns (January, February, March, etc).

try to solve it with a transposed base

{product, month, quantity}
a- january- 100
a- February- 12
a- March- 0
b- January- 0
b- February- 50
b- March- 30
c- January- 100
c- February- 120
c- March- 40

using lastnonblank/lastnonblankvalue but the result is sometimes wrong

could you guide me to get the following


{product ,lastvaluequantity ,lastnameornumberofmonth}
a- 12- february/2
b- 30- march/4
c- 40- march/4

 

also in the future I would include another parameter(column) which is year ,but at moment need the first consult.

 

 

Thanks a lot

 

 

 

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

Hi @aguirre_2022 ,

Please refer to my pbix file to see if it helps you.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUTI0MACRRkDCQClWJ1opCcgCIlOQsDFEKBlJHYg0AQrHAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [product = _t, January = _t, Febuary = _t, March = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"product", type text}, {"January", Int64.Type}, {"Febuary", Int64.Type}, {"March", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"product"}, "Attribute", "Value"),
    #"Added Conditional Column" = Table.AddColumn(#"Unpivoted Columns", "Custom", each if [Value] = 0 then "" else [Value]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Conditional Column",{"Value"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "value"}}),
    #"Filtered Rows" = Table.SelectRows(#"Renamed Columns", each [value] <> null and [value] <> ""),
    #"Renamed Columns1" = Table.RenameColumns(#"Filtered Rows",{{"Attribute", "month"}}),
    #"Added Index" = Table.AddIndexColumn(#"Renamed Columns1", "Index", 1, 1, Int64.Type),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Index",{{"value", Int64.Type}})
in
    #"Changed Type1"

vrongtiepmsft_0-1688693826369.png

Then create a measure.

Measure =
VAR _1 =
    MAXX (
        FILTER (
            ALL ( 'Table' ),
            'Table'[product] = SELECTEDVALUE ( 'Table'[product] )
        ),
        'Table'[Index]
    )
RETURN
    IF ( MAX ( 'Table'[Index] ) = _1, MAX ( 'Table'[value] ), BLANK () )

Finally filter the measure is bot blank.

vrongtiepmsft_1-1688693894492.png

 

How to Get Your Question Answered Quickly 

 

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

 

Best Regards
Community Support Team _ Rongtie

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

2 REPLIES 2
aguirre_2022
Regular Visitor

it was very helpful, thank you very much

 

I have an additional query, if I want to take this calculated value (last val) and multiply it by a column, then I can take this value (last val) and multiply it by a column.

when using max , min,maxx or minx it gives me a different calculation

A/ last val /expected value
12 5 60
but  when use max or maxx 

A /lastval/result

12 8   96

 

I solved it by exporting to an auxiliary table only the last values of my product.

but I would have to make these auxiliary tables whenever I need a new calculation?

 

Thanks for your time

v-rongtiep-msft
Community Support
Community Support

Hi @aguirre_2022 ,

Please refer to my pbix file to see if it helps you.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUTI0MACRRkDCQClWJ1opCcgCIlOQsDFEKBlJHYg0AQrHAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [product = _t, January = _t, Febuary = _t, March = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"product", type text}, {"January", Int64.Type}, {"Febuary", Int64.Type}, {"March", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"product"}, "Attribute", "Value"),
    #"Added Conditional Column" = Table.AddColumn(#"Unpivoted Columns", "Custom", each if [Value] = 0 then "" else [Value]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Conditional Column",{"Value"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "value"}}),
    #"Filtered Rows" = Table.SelectRows(#"Renamed Columns", each [value] <> null and [value] <> ""),
    #"Renamed Columns1" = Table.RenameColumns(#"Filtered Rows",{{"Attribute", "month"}}),
    #"Added Index" = Table.AddIndexColumn(#"Renamed Columns1", "Index", 1, 1, Int64.Type),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Index",{{"value", Int64.Type}})
in
    #"Changed Type1"

vrongtiepmsft_0-1688693826369.png

Then create a measure.

Measure =
VAR _1 =
    MAXX (
        FILTER (
            ALL ( 'Table' ),
            'Table'[product] = SELECTEDVALUE ( 'Table'[product] )
        ),
        'Table'[Index]
    )
RETURN
    IF ( MAX ( 'Table'[Index] ) = _1, MAX ( 'Table'[value] ), BLANK () )

Finally filter the measure is bot blank.

vrongtiepmsft_1-1688693894492.png

 

How to Get Your Question Answered Quickly 

 

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

 

Best Regards
Community Support Team _ Rongtie

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

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.