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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
SimoniAr80
New Member

Inserting rows for missing values based on different lists

Hi PowerBI Community,

 

I am pretty new to PowerBI and Power Query. 

I would like to add missing values to my Excel files, for the following type of table:

 

testfirst namelast namegradefieldclassroom codepassed?
arithmetics 1BethHenson14Math10.4YES
arithmetics 1ChrisConnemara17Math10.4YES
arithmetics 1EdwardShetland10Math10.4NO
arithmetics 1JohnAppaloosa8Math10.4NO
arithmetics 1JessHalfinger18Math10.4YES
geometry 1BethHenson11Math10.4NO
geometry 1ChrisConnemara13Math10.4YES
geometry 1EdwardShetland15Math10.4YES
geometry 1JohnAppaloosa14Math10.4YES
geometry 1JessHalfinger15Math10.4YES
poetry 1BethHenson10English10.4NO
poetry 1EdwardShetland14English10.4YES
poetry 1JohnAppaloosa13English10.4YES
poetry 1JessHalfinger9English10.4NO
arithmetics 1PeterMalinois14Math10.2YES
arithmetics 1PatriciaGroenendal12Math10.2YES
arithmetics 1GloriaAustralian18Math10.2YES
arithmetics 1MelvinSetter15Math10.2YES
arithmetics 1JessSpaniel11Math10.2NO
geometry 1PeterMalinois10Math10.2NO
geometry 1PatriciaGroenendal16Math10.2YES
geometry 1MelvinSetter13Math10.2YES
geometry 1JessSpaniel16Math10.2YES
poetry 1PeterMalinois13English10.2YES
poetry 1PatriciaGroenendal9English10.2NO
poetry 1GloriaAustralian12English10.2YES
poetry 1MelvinSetter8English10.2NO
poetry 1JessSpaniel9English10.2NO

 

The "passed?" column uses the formula =IF(D2>=12, "YES", "NO"). I then expanded the formula down for all rows.

For the poetry 1 test, Chris Connemara from classroom 10.4 is missing. Also, Gloria Australian's results are missing for the geometry 1 test.

I would like to create a function to add a row for each of this missing values, with the corresponding test, first name and last name, field, classroom code. Moreover, the "grade" would be ABS (for absent) for these students and the "passed?" column would display "NA".

 

Thanks in advance for your help,

Best Regards,

SimoniAr80

1 ACCEPTED SOLUTION

Hi @SimoniAr80 

 

Try the M code below. 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZPRS8MwEMb/FenzkLWdOh+nlA1hOuiTjD0c69kGsqQkUfG/NykK7e5S8tIkhV++7767HI8ZGOG6Czpxtjd5tsie0HV+2aGyWvlNvvKfPQw/8+VtOL1XdXZaUPS5M8KGVSuFFzAQkIdkvGq+wTR+U3foJKhmQAj++sbSL7oLdjd9D1JrG7TXySza4HsH8kOoFk0AKPzvu0XtWfPDx5XHRCcYG1WZJsnmdJfGMinNNHiKMiHFVXsdjyj0tFKtFJamNOLYOlccy4hyhZapLK30ccby9TQd0A3MHqRQemjzVcbFzCs4gDPiLILhrdGoUDUgA1YkX7GV2gwXbD6tM94FKDrRcxfsUX6JwNToHNfpOfgvvboHJVDSJ1FEngQX2zKNjEV2HzU94ZlyyzSS1hqXHA0YVyoZzhgbKZZM6CitER6ZjSJRnUa1TtMlSUX9nn4B", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [test = _t, #"first name" = _t, #"last name" = _t, grade = _t, field = _t, #"classroom code" = _t, #"passed?" = _t]),
    #"Removed Other Columns1" = Table.SelectColumns(Source,{"first name", "last name", "classroom code"}),
    #"Removed Duplicates1" = Table.Distinct(#"Removed Other Columns1"),
    classroom_tests = Table.Distinct(Table.SelectColumns(Source,{"classroom code", "test"})),
    #"Merged Queries" = Table.NestedJoin(#"Removed Duplicates1", {"classroom code"}, classroom_tests, {"classroom code"}, "Table", JoinKind.LeftOuter),
    #"Expanded Table" = Table.ExpandTableColumn(#"Merged Queries", "Table", {"test"}, {"test"}),
    Custom1 = Table.NestedJoin(#"Expanded Table", {"first name", "last name", "classroom code", "test"}, Source, {"first name", "last name", "classroom code", "test"}, "DataTable", JoinKind.LeftOuter),
    #"Expanded DataTable" = Table.ExpandTableColumn(Custom1, "DataTable", {"grade", "field", "passed?"}, {"grade", "field", "passed?"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded DataTable",{"test", "first name", "last name", "grade", "field", "classroom code", "passed?"}),
    #"Sorted Rows" = Table.Sort(#"Reordered Columns",{{"classroom code", Order.Ascending}, {"test", Order.Ascending}, {"field", Order.Descending}}),
    #"Filled Down" = Table.FillDown(#"Sorted Rows",{"field"}),
    #"Replaced Value" = Table.ReplaceValue(#"Filled Down",null,"ABS",Replacer.ReplaceValue,{"grade"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value",null,"NA",Replacer.ReplaceValue,{"passed?"})
in
    #"Replaced Value1"

 

Note that when you come to below step, sort the table by columns "classroom code", "test", "field" one by one (don't change this sort order). Then you will see the null rows display below the same test in the same classroom. Then use fill down feature to fill in them. 

21090302.jpg

 

Final result

21090303.jpg

 

Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

View solution in original post

4 REPLIES 4
SimoniAr80
New Member

Here is the Excel table I would like to obtain after the insertions:

testfirst namelast namegradefieldclassroom codepassed?
arithmetics 1BethHenson14Math10.4YES
arithmetics 1ChrisConnemara17Math10.4YES
arithmetics 1EdwardShetland10Math10.4NO
arithmetics 1JohnAppaloosa8Math10.4NO
arithmetics 1JessHalfinger18Math10.4YES
geometry 1BethHenson11Math10.4NO
geometry 1ChrisConnemara13Math10.4YES
geometry 1EdwardShetland15Math10.4YES
geometry 1JohnAppaloosa14Math10.4YES
geometry 1JessHalfinger15Math10.4YES
poetry 1BethHenson10English10.4NO
poetry 1ChrisConnemaraABSEnglish10.4NA
poetry 1EdwardShetland14English10.4YES
poetry 1JohnAppaloosa13English10.4YES
poetry 1JessHalfinger9English10.4NO
arithmetics 1PeterMalinois14Math10.2YES
arithmetics 1PatriciaGroenendal12Math10.2YES
arithmetics 1GloriaAustralian18Math10.2YES
arithmetics 1MelvinSetter15Math10.2YES
arithmetics 1JessSpaniel11Math10.2NO
geometry 1PeterMalinois10Math10.2NO
geometry 1PatriciaGroenendal16Math10.2YES
geometry 1GloriaAustralianABSMath10.2NA
geometry 1MelvinSetter13Math10.2YES
geometry 1JessSpaniel16Math10.2YES
poetry 1PeterMalinois13English10.2YES
poetry 1PatriciaGroenendal9English10.2NO
poetry 1GloriaAustralian12English10.2YES
poetry 1MelvinSetter8English10.2NO
poetry 1JessSpaniel9English10.2NO

Here's one way to do it with a self merge in the query editor.  To see how it works, just create a blank query, open the Advanced Editor and replace the text there with the M code below.  The field and classroom fields are null in the added rows.  If those are constant values for those tests, this code could be adapted to use those values.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZPRS8MwEMb/FenzkLWdOh+nlA1hOuiTjD0c69kGsqQkUfG/NykK7e5S8tIkhV++7767HI8ZGOG6Czpxtjd5tsie0HV+2aGyWvlNvvKfPQw/8+VtOL1XdXZaUPS5M8KGVSuFFzAQkIdkvGq+wTR+U3foJKhmQAj++sbSL7oLdjd9D1JrG7TXySza4HsH8kOoFk0AKPzvu0XtWfPDx5XHRCcYG1WZJsnmdJfGMinNNHiKMiHFVXsdjyj0tFKtFJamNOLYOlccy4hyhZapLK30ccby9TQd0A3MHqRQemjzVcbFzCs4gDPiLILhrdGoUDUgA1YkX7GV2gwXbD6tM94FKDrRcxfsUX6JwNToHNfpOfgvvboHJVDSJ1FEngQX2zKNjEV2HzU94ZlyyzSS1hqXHA0YVyoZzhgbKZZM6CitER6ZjSJRnUa1TtMlSUX9nn4B", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [test = _t, #"first name" = _t, #"last name" = _t, grade = _t, field = _t, #"classroom code" = _t, #"passed?" = _t]),
    #"Removed Other Columns" = Table.SelectColumns(Source,{"first name", "last name"}),
    #"Removed Duplicates" = Table.Distinct(#"Removed Other Columns"),
    #"Added Custom" = Table.AddColumn(#"Removed Duplicates", "test", each List.Distinct(Source[test])),
    #"Expanded test" = Table.ExpandListColumn(#"Added Custom", "test"),
    #"Merged Queries" = Table.NestedJoin(#"Expanded test", {"first name", "last name", "test"}, Source, {"first name", "last name", "test"}, "Expanded test", JoinKind.LeftOuter),
    #"Expanded Expanded test" = Table.ExpandTableColumn(#"Merged Queries", "Expanded test", {"grade", "field", "classroom code", "passed?"}, {"grade", "field", "classroom code", "passed?"}),
    #"Replaced Value" = Table.ReplaceValue(#"Expanded Expanded test",null,"NA",Replacer.ReplaceValue,{"grade"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value",null,"ABS",Replacer.ReplaceValue,{"passed?"})
in
    #"Replaced Value1"

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Hi Pat,

 

Thanks for your help and taking some time for my question.

Your response partly answered: 

To fill in the null values in the added rows, I would like to use a conditionnal formula but the issue I have is that there are several classrooms with different students associated in the same file: I don't see how a simple conditional formula could replace those null values by the correct "classroom code" and "field".

Do you know a way to fill:

  • the "field" column with "Math" when the "test" column contains arithmetic or geometry, or English when the "test" column contains "poetry"?
  • the "classroom code" with 10.2 if last name is "Malinois" or "Groenendal" or "Australian" or "Setter" or "Spaniel"; 10.4 for another list of last names; etc.

Thanks,
Best Regards.

Hi @SimoniAr80 

 

Try the M code below. 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZPRS8MwEMb/FenzkLWdOh+nlA1hOuiTjD0c69kGsqQkUfG/NykK7e5S8tIkhV++7767HI8ZGOG6Czpxtjd5tsie0HV+2aGyWvlNvvKfPQw/8+VtOL1XdXZaUPS5M8KGVSuFFzAQkIdkvGq+wTR+U3foJKhmQAj++sbSL7oLdjd9D1JrG7TXySza4HsH8kOoFk0AKPzvu0XtWfPDx5XHRCcYG1WZJsnmdJfGMinNNHiKMiHFVXsdjyj0tFKtFJamNOLYOlccy4hyhZapLK30ccby9TQd0A3MHqRQemjzVcbFzCs4gDPiLILhrdGoUDUgA1YkX7GV2gwXbD6tM94FKDrRcxfsUX6JwNToHNfpOfgvvboHJVDSJ1FEngQX2zKNjEV2HzU94ZlyyzSS1hqXHA0YVyoZzhgbKZZM6CitER6ZjSJRnUa1TtMlSUX9nn4B", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [test = _t, #"first name" = _t, #"last name" = _t, grade = _t, field = _t, #"classroom code" = _t, #"passed?" = _t]),
    #"Removed Other Columns1" = Table.SelectColumns(Source,{"first name", "last name", "classroom code"}),
    #"Removed Duplicates1" = Table.Distinct(#"Removed Other Columns1"),
    classroom_tests = Table.Distinct(Table.SelectColumns(Source,{"classroom code", "test"})),
    #"Merged Queries" = Table.NestedJoin(#"Removed Duplicates1", {"classroom code"}, classroom_tests, {"classroom code"}, "Table", JoinKind.LeftOuter),
    #"Expanded Table" = Table.ExpandTableColumn(#"Merged Queries", "Table", {"test"}, {"test"}),
    Custom1 = Table.NestedJoin(#"Expanded Table", {"first name", "last name", "classroom code", "test"}, Source, {"first name", "last name", "classroom code", "test"}, "DataTable", JoinKind.LeftOuter),
    #"Expanded DataTable" = Table.ExpandTableColumn(Custom1, "DataTable", {"grade", "field", "passed?"}, {"grade", "field", "passed?"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded DataTable",{"test", "first name", "last name", "grade", "field", "classroom code", "passed?"}),
    #"Sorted Rows" = Table.Sort(#"Reordered Columns",{{"classroom code", Order.Ascending}, {"test", Order.Ascending}, {"field", Order.Descending}}),
    #"Filled Down" = Table.FillDown(#"Sorted Rows",{"field"}),
    #"Replaced Value" = Table.ReplaceValue(#"Filled Down",null,"ABS",Replacer.ReplaceValue,{"grade"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value",null,"NA",Replacer.ReplaceValue,{"passed?"})
in
    #"Replaced Value1"

 

Note that when you come to below step, sort the table by columns "classroom code", "test", "field" one by one (don't change this sort order). Then you will see the null rows display below the same test in the same classroom. Then use fill down feature to fill in them. 

21090302.jpg

 

Final result

21090303.jpg

 

Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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