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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
xrodpim
New Member

How to use M code to analyze a dataset and put more new rows into it, depending of its current data?


Hi everyone !

I´m a beginner with Power Query and I have studied about M code. I saw how to transform my dataset and put new rows into it. For example, I can do something like this:

= Table.InsertRows( #"New Rows", 0, {[#"Date"= "01/01/1979", #"Item"="LTE", #"Built"="100", ]} )

 

My dataset has several kinds of itens and many rows. But a simple example is like this:

DateItembuilt
10/10/2021GSM100%
11/10/2021LTE100%
12/10/2021TDMA100%
15/01/2022LTE100%
17/01/2022TDMA100%

 

In the table above, the GSM Item is absent for January/2022. In this case I have to put one more row in the table, with values ​​like this:

 

01/01/2022GSM-3

 

This idea is the transformation I need to implement. However, I still can't figure out how to read my entire dataset, parsing it, and deciding to use Table.InsertRows.

 

How can I use M code to figure out what itens is not present for wich months? I´m guessing I have to do some kind of loop in my code to read all rows of my dataset and determine what itens is not present for what months. But, can I codify such logics in M code? Should I use advanved editor? I even don´t know yet if such logic must be placed between a 'let 'and a 'in'.

Can someone here give me some idea about it , to clarify it ? My question is mainly about M code example. How to codify it?

Best regards.

Rodrigo Pimenta Carvalho

 

 

1 ACCEPTED SOLUTION
ronrsnfld
Super User
Super User

  • Create a list of all the Items
  • Group By "Year-Month"
    • Examine each subtable to create a list of the missing Items
  • Use List.Accumulate to create a table of the missing items
  • Combine that table with the original subtable.

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjTQByIjAyNDJR0l92BfIGloYKCqFKsDlDNEkvMJcUWRM0KSC3HxdUSRNNU3MARJGmHRaI4kh6IxFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Item = _t, built = _t]),
    typeIt = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Item", type text}, {"built", Percentage.Type}}, "en-GB"),

//added a row in a new month with only a single Item for testing 
//  when multiple items are missing from a month
    #"Previous Step" = Table.InsertRows(typeIt,Table.RowCount(typeIt),{[Date=#date(2022,3,1), Item="GSM", built=1]}),

//Replace above steps with however you actually set up your data

    #"All Items" = List.Distinct(#"Previous Step"[Item]),

//Group by month-year
// generate a list of missing items in each month
// as well as the original sub-table
    #"Add Year-Month Column" = Table.AddColumn(#"Previous Step", "Year-Month", each Date.ToText([Date], Date.ToText([Date],"yyyy-MM")),type text),
    #"Group by Year-Month" = Table.Group(#"Add Year-Month Column","Year-Month",{
        {"missing",
        (t)=> List.RemoveNulls(
        List.Generate(
            ()=>[pos=List.PositionOf(t[Item],#"All Items"{0}), idx=0],
            each [idx] < List.Count(#"All Items"),
            each [pos=List.PositionOf(t[Item],#"All Items"{[idx]+1}), idx=[idx]+1],
            each if [pos] = -1 then #"All Items"{[idx]} else null)
        )},
        {"All", each _, type table[Date=date, Item=text, built=Percentage.Type]}
    }),

//add custom column with the added rows for each month
    #"Added Custom" = Table.AddColumn(#"Group by Year-Month", "Added Rows", 
        each Table.Combine({
            [All],
            Table.FromRecords(
                List.Accumulate([missing], {}, (state, current)=> state & 
                    {[Date=Date.From([#"Year-Month"]), Item=current, built=-3]})
            )}
            )
            ),

//Remove unneccesary columns
//Expand and type the new table columns
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Year-Month", "missing", "All"}),
    #"Expanded Added Rows" = Table.ExpandTableColumn(#"Removed Columns", "Added Rows", {"Date", "Item", "built"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Added Rows",{{"Date", type date}, {"Item", type text}, {"built", Percentage.Type}})
in
    #"Changed Type"

 

Before

ronrsnfld_0-1656111583204.png

After

ronrsnfld_1-1656111620836.png

 

 

View solution in original post

1 REPLY 1
ronrsnfld
Super User
Super User

  • Create a list of all the Items
  • Group By "Year-Month"
    • Examine each subtable to create a list of the missing Items
  • Use List.Accumulate to create a table of the missing items
  • Combine that table with the original subtable.

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjTQByIjAyNDJR0l92BfIGloYKCqFKsDlDNEkvMJcUWRM0KSC3HxdUSRNNU3MARJGmHRaI4kh6IxFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Item = _t, built = _t]),
    typeIt = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Item", type text}, {"built", Percentage.Type}}, "en-GB"),

//added a row in a new month with only a single Item for testing 
//  when multiple items are missing from a month
    #"Previous Step" = Table.InsertRows(typeIt,Table.RowCount(typeIt),{[Date=#date(2022,3,1), Item="GSM", built=1]}),

//Replace above steps with however you actually set up your data

    #"All Items" = List.Distinct(#"Previous Step"[Item]),

//Group by month-year
// generate a list of missing items in each month
// as well as the original sub-table
    #"Add Year-Month Column" = Table.AddColumn(#"Previous Step", "Year-Month", each Date.ToText([Date], Date.ToText([Date],"yyyy-MM")),type text),
    #"Group by Year-Month" = Table.Group(#"Add Year-Month Column","Year-Month",{
        {"missing",
        (t)=> List.RemoveNulls(
        List.Generate(
            ()=>[pos=List.PositionOf(t[Item],#"All Items"{0}), idx=0],
            each [idx] < List.Count(#"All Items"),
            each [pos=List.PositionOf(t[Item],#"All Items"{[idx]+1}), idx=[idx]+1],
            each if [pos] = -1 then #"All Items"{[idx]} else null)
        )},
        {"All", each _, type table[Date=date, Item=text, built=Percentage.Type]}
    }),

//add custom column with the added rows for each month
    #"Added Custom" = Table.AddColumn(#"Group by Year-Month", "Added Rows", 
        each Table.Combine({
            [All],
            Table.FromRecords(
                List.Accumulate([missing], {}, (state, current)=> state & 
                    {[Date=Date.From([#"Year-Month"]), Item=current, built=-3]})
            )}
            )
            ),

//Remove unneccesary columns
//Expand and type the new table columns
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Year-Month", "missing", "All"}),
    #"Expanded Added Rows" = Table.ExpandTableColumn(#"Removed Columns", "Added Rows", {"Date", "Item", "built"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded Added Rows",{{"Date", type date}, {"Item", type text}, {"built", Percentage.Type}})
in
    #"Changed Type"

 

Before

ronrsnfld_0-1656111583204.png

After

ronrsnfld_1-1656111620836.png

 

 

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

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