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
prateekraina
Memorable Member
Memorable Member

Find difference of last two not null values

Hi Guys,

 

I need to find out difference of last two not null values in M Query.
Below table will explain what i require:

    What i require
XYZIndexXval
nullnullnullnullnull
nullnullnullnullnull
1251null or 0
nullnullnullnullnull
nullnullnullnullnull
31122
2683-1
nullnullnullnull 
44442


i have tried below M Query but i am not getting desired result. Kindly help.

= Table.AddColumn(#"Changed Type1", "xval", each if [Index] > 0 then [x] - #"Changed Type1"{[Index] - 1}[x] else 0)


@ImkeF Hope you can solve this as well 🙂

Prateek Raina

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

Pretty close again 😉 :

 

=Table.AddColumn(ChgType, "xval", each try [x]-#"Changed Type1"{[Index=[Index]-1]}[x] otherwise null)

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

11 REPLIES 11
ImkeF
Super User
Super User

Pretty close again 😉 :

 

=Table.AddColumn(ChgType, "xval", each try [x]-#"Changed Type1"{[Index=[Index]-1]}[x] otherwise null)

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi @ImkeF,

 

Is there any other way of achieveing this? Performance wise it is taking very long to refresh the report.

 

Prateek Raina

Yes, that might be the case.

 

You can use shifted index then: Add another column where you calculate "PreviousRowIndex": [Index]-1

 

Then join that with itself on both indices:

 

Table.NestedJoin(<PreviousStepName>,{"Index"}, <PreviousStepName> ,{"PreviousRowIndex"},"NewColumn",JoinKind.LeftOuter)

Expand X and calculate difference to existing X

 

This screencast shows how to join a table with itself: https://www.youtube.com/watch?v=looCm3cbINw

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi @ImkeF,

 

Can you please elaborate these steps as i have recently started learning M.
I have published a sample report which has all the steps i have done so far, it would be really helpful if you could edit the same and share back.
Link: https://drive.google.com/drive/folders/0B2-x82_nA30gcDhKRzFQZ1hxSTQ?usp=sharing

 

Thanks for your patience.

 

Prateek Raina

That's understandable, as we have a double-merge now 🙂

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WyivNyVHSQaFidXAKGwI5RkBsik8RDmFjIMcQjEE8kClmQGyBT4sJkAPBsbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [X = _t, Y = _t, Z = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"X", Int64.Type}, {"Y", Int64.Type}, {"Z", Int64.Type}}),
    RowNumber = Table.AddIndexColumn(#"Changed Type", "RowNumber", 0, 1),
    #"Filtered Rows" = Table.SelectRows(RowNumber, each ([X] <> null)),
    #"Added Index" = Table.AddIndexColumn(#"Filtered Rows", "Index", 1, 1),
    SecondIndex = Table.AddColumn(#"Added Index", "IndexPrevRow", each [Index]+1),
    MergeForPrevRow = Table.NestedJoin(SecondIndex,{"Index"},SecondIndex,{"IndexPrevRow"},"NewColumn",JoinKind.LeftOuter),
    #"Expanded NewColumn1" = Table.ExpandTableColumn(MergeForPrevRow, "NewColumn", {"X"}, {"X.1"}),
    DiffX = Table.AddColumn(#"Expanded NewColumn1", "DiffX", each [X]-[X.1]),
    MergeBackToMainTable = Table.NestedJoin(RowNumber,{"RowNumber"},DiffX,{"RowNumber"},"NewColumn",JoinKind.LeftOuter),
    #"Expanded NewColumn" = Table.ExpandTableColumn(MergeBackToMainTable, "NewColumn", {"Index", "DiffX"}, {"Index", "DiffX"})
in
    #"Expanded NewColumn"

Pls let me know if I have to upload the file as well.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi @ImkeF,

 

Thanks for the explanation, will surely try it out tomorrow.

Just adding a step further to this problem (which for sure is the last step 😄 ), can you tell me if i have additional column (SensorKey) as depicted in table below and i want to reiterate this logic once the key changes, what logic should be added.

Sensor KeyXYZXval
1nullnullnullnull
1125null
1nullnullnullnull
13112
1268-1
1nullnullnullnull
14442
2nullnullnullnull
2125null
2nullnullnullnull
23112
2268-1
2nullnullnullnull
24442

 

Prateek Raina

Yes, thats easy. Will post solution tomorrow.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Great!! 🙂 Will look forward to it. Thanks a bunch 🙂

You "partition" your table into one table per SensorKey and apply the logic that we have so far. In order to do so, you transform the query into a function.

 

This is the full query code with sample data:

 

let

Function = (Table) =>
let
    RowNumber = Table.AddIndexColumn(Table, "RowNumber", 0, 1),
    #"Filtered Rows" = Table.SelectRows(RowNumber, each ([X] <> null)),
    #"Added Index" = Table.AddIndexColumn(#"Filtered Rows", "Index", 1, 1),
    SecondIndex = Table.AddColumn(#"Added Index", "IndexPrevRow", each [Index]+1),
    MergeForPrevRow = Table.NestedJoin(SecondIndex,{"Index"},SecondIndex,{"IndexPrevRow"},"NewColumn",JoinKind.LeftOuter),
    #"Expanded NewColumn1" = Table.ExpandTableColumn(MergeForPrevRow, "NewColumn", {"X"}, {"X.1"}),
    DiffX = Table.AddColumn(#"Expanded NewColumn1", "DiffX", each [X]-[X.1]),
    MergeBackToMainTable = Table.NestedJoin(RowNumber,{"RowNumber"},DiffX,{"RowNumber"},"NewColumn",JoinKind.LeftOuter),
    #"Expanded NewColumn" = Table.ExpandTableColumn(MergeBackToMainTable, "NewColumn", {"Index", "DiffX"}, {"Index", "DiffX"})
in
    #"Expanded NewColumn",

    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUcorzcnBTsXqQFSAsBEQm6JLENZqDNUOMQImCjLMDIgtgFjXkATjTJAwxDgjgvqMcPmAOK2YPjDC7gPijEPzQSwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Sensor Key" = _t, X = _t, Y = _t, Z = _t, Xval = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Sensor Key", Int64.Type}, {"X", Int64.Type}, {"Y", Int64.Type}, {"Z", Int64.Type}, {"Xval", Int64.Type}}),
    PartitionTable = Table.Group(#"Changed Type", {"Sensor Key"}, {{"Partition", each _, type table}}),
    CallFunctionForEachTable = Table.AddColumn(PartitionTable, "TransformedTable", each Function([Partition])),
    Cleanup = Table.RemoveColumns(CallFunctionForEachTable,{"Partition"}),
    Expand = Table.ExpandTableColumn(Cleanup, "TransformedTable", {"X", "Y", "Z", "Xval", "RowNumber", "Index", "DiffX"}, {"X", "Y", "Z", "Xval", "RowNumber", "Index", "DiffX"})
in
    Expand

and here the file to play around: https://1drv.ms/u/s!Av_aAl3fXRbehasMiQjFM0wV8VHV8A

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Thanks for all the help @ImkeF. Really appreciate it!!

 

Prateek Raina

Thanks a lot @ImkeF. You are superb !!

Prateek Raina

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.