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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

Dataset Filtering by List First and Column Value

Hello guys,

How is everything?

 

Checking the below tables, I put the current dataset (but the provided is just a sample not the complete dataset) vs. the desired output.

 

What I aim to do is:

  • To filter column: Code to have only:
  1. The first row in the list of columns: Name and Data, bearing in mind that the first row’s value is not fixed)
  2. Any value that is: Break
  3. Any value that is Meal
  • To rename the first break on a day in column: Code as: First Break and the second break on a day in column: Code as: Second Break, bearing in mind that mostly the employee has 2 breaks, but on some days, there might be 1 break or even none.

 

How I could achieve that? Any insights?

1 ACCEPTED SOLUTION
watkinnc
Super User
Super User

I would break this into 3 queries, then append all three.

 

First, I would make three new blank queries, named First, Breaks, and Meals.  Suppose your main table is named FactTable. 

For the First query, in the formula bar, you want to group the FactTable. I can't quite be 100% sure on the syntax because I'm not at my desk, but its:

 

=Table.Group(FactTable,//and group by Name and Date.  Just use the GUI button, and be sure to select an aggregation -- All Rows. Name that column "Details".

 

Next, add a column to get each first row:

 

= Table.AddColumn(Source, "FirstRows", each [Details]{0})

 

You can now delete your original columns and then expand the Details column.

 

For the Breaks query, in the formula bar, type:

 

= Table.SelectRows(FactTable, each [Code] = "Break")

Then group this table by Name and Date, also using the All Rows aggregation, also named "Details". But for the multiple breaks per

day, we need to add an index column to each nested table:

 

= Table.TransformColumns(Source, {{"Details", each Table.AddIndexColumn([Details], "Index" 1,1, type number)}})

 

Now you can get rid of your original columns and then expand your Drtails column. Now you can transform the Code column:

 

= Table.TransformColumns(#"Removed Columns", {{"Code", each Text.Insert([Code], 0, if [Index] = 1 then "First " else if [Index] = 2 then "Second " else if [Index] = 3 then "Third " else "Fourth ")}})

 

Now you can remove the Index Column. 

The Lunch query is straightforward:

 

= Table.SelectRows(FactTable, each [Code] = "Meal")

 

Now that you have all three tables, double check to make sure that the column names and types are the same, and then you can make your final query. Add a blank query, and in the Advanced Editor, type:

 

let

EmptyTable = Table.FromRows({{null, null, null}}, type table [Name = text, Code = text, Date = date]),

FirstTable = if Table.IsEmpty(First) then EmptyTable else First,

SecondTable = if Table.IsEmpty(Breaks) then EmptyTable else Breaks,

ThirdTable = if Table.IsEmpty(Meals) then EmptyTable else Meals,

FinalTable = Table.Combine({FirstTable, SecondTable, ThirdTable})

in FinalTable

 

That should do it!

 

--Nate

 

 


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!

View solution in original post

5 REPLIES 5
watkinnc
Super User
Super User

I would break this into 3 queries, then append all three.

 

First, I would make three new blank queries, named First, Breaks, and Meals.  Suppose your main table is named FactTable. 

For the First query, in the formula bar, you want to group the FactTable. I can't quite be 100% sure on the syntax because I'm not at my desk, but its:

 

=Table.Group(FactTable,//and group by Name and Date.  Just use the GUI button, and be sure to select an aggregation -- All Rows. Name that column "Details".

 

Next, add a column to get each first row:

 

= Table.AddColumn(Source, "FirstRows", each [Details]{0})

 

You can now delete your original columns and then expand the Details column.

 

For the Breaks query, in the formula bar, type:

 

= Table.SelectRows(FactTable, each [Code] = "Break")

Then group this table by Name and Date, also using the All Rows aggregation, also named "Details". But for the multiple breaks per

day, we need to add an index column to each nested table:

 

= Table.TransformColumns(Source, {{"Details", each Table.AddIndexColumn([Details], "Index" 1,1, type number)}})

 

Now you can get rid of your original columns and then expand your Drtails column. Now you can transform the Code column:

 

= Table.TransformColumns(#"Removed Columns", {{"Code", each Text.Insert([Code], 0, if [Index] = 1 then "First " else if [Index] = 2 then "Second " else if [Index] = 3 then "Third " else "Fourth ")}})

 

Now you can remove the Index Column. 

The Lunch query is straightforward:

 

= Table.SelectRows(FactTable, each [Code] = "Meal")

 

Now that you have all three tables, double check to make sure that the column names and types are the same, and then you can make your final query. Add a blank query, and in the Advanced Editor, type:

 

let

EmptyTable = Table.FromRows({{null, null, null}}, type table [Name = text, Code = text, Date = date]),

FirstTable = if Table.IsEmpty(First) then EmptyTable else First,

SecondTable = if Table.IsEmpty(Breaks) then EmptyTable else Breaks,

ThirdTable = if Table.IsEmpty(Meals) then EmptyTable else Meals,

FinalTable = Table.Combine({FirstTable, SecondTable, ThirdTable})

in FinalTable

 

That should do it!

 

--Nate

 

 


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!
Anonymous
Not applicable

You gave me fantastic insights of the solution sequence, yes some of the provided scripts needed some search to correct them, but this is even better and more interesting to learn the right code by searching here and there, what I needed the most was directions of steps of what to do, which you have provided perfectly, you are truly good instructor, thanks a million.

Thank you very much!!


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!
Anonymous
Not applicable

NameCodeDate

First PersonLogin8/20/2021
First PersonFirst Break8/20/2021
Second PersonSystem8/24/2021
Second PersonFirst Break8/24/2021
Second PersonMeal8/24/2021
Second PersonSecond Break8/24/2021
Anonymous
Not applicable

NameCodeDate

First PersonLogin8/20/2021
First PersonWork8/20/2021
First PersonBreak8/20/2021
First PersonWork8/20/2021
First PersonNot Available8/20/2021
Second PersonSystem8/24/2021
Second PersonWork8/24/2021
Second PersonBreak8/24/2021
Second PersonWork8/24/2021
Second PersonMeal8/24/2021
Second PersonSystem8/24/2021
Second PersonWork8/24/2021
Second PersonBreak8/24/2021
Second PersonWork8/24/2021

Helpful resources

Announcements
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

Top Solution Authors
Top Kudoed Authors