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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Zyg_D
Continued Contributor
Continued Contributor

M code to get value which was active on specific date

This is my biiiiig table. It holds all the changes made to a database table together with the date. 

data.JPG

I don't need to import the whole table. I only need the "Status" value of every "Name" on two specific dates - 20190902 and 20191002. This is how the table should look after importing it: 

result.JPG

What M code would do it? 

1 ACCEPTED SOLUTION
Zyg_D
Continued Contributor
Continued Contributor

After the whole day of thinking, trial and error, I came up with something acceptable. Thes M code is not as difficult as DAX. I like it. Kudos to @SteveCampbell  who encouraged me to try out UI. 

 

let
    Source = Excel.Workbook(File.Contents("C:\Temp\PBI_example.xlsx"), null, true),
    Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"Date", Int64.Type}, {"Name", type text}, {"Status", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "DateGroup", each if [Date] <= 20191002 then
   if [Date] <= 20190902
   then 20190902
   else 20191002
   else 0),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Added Custom", {{"Date", type text}}, "en-US"),{"Date", "Status"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"DateStatus"),
    #"Grouped Rows" = Table.Group(#"Merged Columns", {"Name", "DateGroup"}, {{"MaxDateStatus", each List.Max([DateStatus]), type text}}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Grouped Rows", "MaxDateStatus", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"MaxDateStatus.1", "MaxDateStatus.2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"MaxDateStatus.1", Int64.Type}, {"MaxDateStatus.2", type text}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"MaxDateStatus.1"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"DateGroup", "Name", "MaxDateStatus.2"})
in
    #"Reordered Columns"

 

result_my.JPG

This shows the status only if it was changed from previous timestamp. I will be able to calculate the remaining one status using Power BI. Using this M code I will only import the data which I need, avoiding tons of useless rows. 

View solution in original post

5 REPLIES 5
Zyg_D
Continued Contributor
Continued Contributor

After the whole day of thinking, trial and error, I came up with something acceptable. Thes M code is not as difficult as DAX. I like it. Kudos to @SteveCampbell  who encouraged me to try out UI. 

 

let
    Source = Excel.Workbook(File.Contents("C:\Temp\PBI_example.xlsx"), null, true),
    Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"Date", Int64.Type}, {"Name", type text}, {"Status", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "DateGroup", each if [Date] <= 20191002 then
   if [Date] <= 20190902
   then 20190902
   else 20191002
   else 0),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Added Custom", {{"Date", type text}}, "en-US"),{"Date", "Status"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"DateStatus"),
    #"Grouped Rows" = Table.Group(#"Merged Columns", {"Name", "DateGroup"}, {{"MaxDateStatus", each List.Max([DateStatus]), type text}}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Grouped Rows", "MaxDateStatus", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"MaxDateStatus.1", "MaxDateStatus.2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"MaxDateStatus.1", Int64.Type}, {"MaxDateStatus.2", type text}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"MaxDateStatus.1"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"DateGroup", "Name", "MaxDateStatus.2"})
in
    #"Reordered Columns"

 

result_my.JPG

This shows the status only if it was changed from previous timestamp. I will be able to calculate the remaining one status using Power BI. Using this M code I will only import the data which I need, avoiding tons of useless rows. 

amitchandak
Super User
Super User

@Zyg_D , while adding a connection from the database you can give a query(under advance property). For this table create a connection and add the query.

AdvanceProperty.png

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Zyg_D
Continued Contributor
Continued Contributor


@amitchandak wrote:

@Zyg_D, while adding a connection from the database you can give a query(under advance property). For this table create a connection and add the query.

AdvanceProperty.png


Hello, @amitchandak  . Thank you for the screenshot. The primary source of the data is indeed the database. However, I get this data from a Power BI dataflow. This option does not allow me to input SQL query. I suppose, the only option I have is M code? I want to stress that I do not want to import the whole table into my model. 

SteveCampbell
Memorable Member
Memorable Member

You shouldn't need to write the code specifically, load it into the UI and user the filters on the column headers.

 



Did I answer your question? Mark my post as a solution! Proud to be a Super User!


Connect with me!
Stay up to date on  
Read my blogs on  



Zyg_D
Continued Contributor
Continued Contributor


@SteveCampbell wrote:

You shouldn't need to write the code specifically, load it into the UI and user the filters on the column headers.

 


You may be right. I ask for assistance, because I am failing to create the desired table on my own. It is not a simple filter. It must also take into account data on another column... I tried grouping and sorting on several columns, but so far no luck. I would greatly appreciate your help. 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.