Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

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

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

Anonymous
Not applicable

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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.