Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
I have a table in Power Query called 1egh TSQL. The table has these 6 columns and data types: "FHA Number", type text, "Property Name", type text, "Decision Type", type text, "Decision Date", type date, "dap_status_id", Int64.Type, "Sequence", Int64.Type.
I want to add a column called “Include_Exclude” that is based on the following:
The "Include_Exclude" column in the sample table shows what the correct response should be. Can you please help me add this "Include_Exclude" column using Power Query?
FHA Number | Property Name | Decision Type | Decision Date | dap_status_id | Sequence | Include_Exclude |
11336007 | A | Firm App Recd | 8/30/2022 | 1478 | 1 | Exclude |
11336007 | A | Firm Prelim Rej/ Defcy Ltr | 9/22/2022 | 1590 | 2 | Exclude |
11336007 | A | Firm Finally Rejected | 12/12/2022 | 1700 | 3 | Include |
08535606 | B | Firm App Recd | 2/11/2022 | 1230 | 1 | Exclude |
08535607 | B | Firm Prelim Rej/ Defcy Ltr | 4/13/2022 | 1234 | 2 | Exclude |
08535608 | B | Firm Finally Rejected | 6/1/2022 | 1320 | 3 | Include |
08535609 | B | Reopen Firm Final Reject | 9/30/2022 | 1450 | 4 | Exclude |
08535610 | B | Firm Finally Rejected | 1/13/2023 | 1555 | 5 | Exclude |
11230077 | C | Firm App Recd | 8/5/2022 | 1279 | 1 | Exclude |
11230077 | C | Firm Prelim Rej/ Defcy Ltr | 9/2/2022 | 1300 | 2 | Exclude |
11230077 | C | Firm Finally Rejected | 9/3/2022 | 1456 | 3 | Include |
11230077 | C | Reopen Firm Final Reject | 9/4/2022 | 1598 | 4 | Exclude |
11230077 | C | Firm Issued | 9/6/2022 | 1609 | 5 | Include |
Hi @PBInewbie17
This code does I believe what your text says, but it does not return your table of expected data. My column is the Include_Exclude2 column.
Example:
Your second to last row for FHA returns Exclude. Mine returns Include. That is because your 2nd bullet point says "if the FHA is a duplicate (meaning the FHA column has the number more than 1 time), and the Decision Type has Firm Finally Rejected and Firm Issued, then everything for that duplicated FHA should be include. So mine returns Include. For every 11230077. You incorrectly show Exclude for 3 records of that FHA#, at least per your decription.
Here is my code, which does a TON of list manipulation to do this.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZLLasMwEEV/RXgd0OgtL9NHINBFyTZkURwVHBzXOAk0f99RbCxRT9IuhrEkfLj3zmy3hRBKWQBXLIol1qruj2zZdWwTqj2ePVfAJUiJ30I7HxvW63fVXPah2C1ownsfmvqIkANnL+GzurK3c4+PJZdywpkSsMl/4FZ1+9E018gL1TlEYUJykVAOIkphrduE8kYZCxZvnwhrCBATQCogrA0AlwPuOdNcqIymCWcDzec0wpjlSZWS922VI2cTvrrQsoQbYbe08+GZyNKUJgF/aBKjOXUbmzHYzGxsGCG4mNUzuUcmpeNKco3mgAdrlDICeovmNMIYJpQFZImwf4Eepq2z1fZE2JSo9el0GaXY6fdhuiaXsvsB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"FHA Number" = _t, #"Property Name" = _t, #"Decision Type" = _t, #"Decision Date" = _t, dap_status_id = _t, Sequence = _t, Include_Exclude = _t]),
#"Removed Other Columns" = Table.SelectColumns(Source,{"FHA Number", "Decision Type"}),
#"Grouped Rows" =
Table.Group(
#"Removed Other Columns",
{"FHA Number"},
{
{"Decision Type List", each _[Decision Type], type list}}
),
#"Merged Source with Grouped Rows" = Table.NestedJoin(Source, {"FHA Number"}, #"Grouped Rows", {"FHA Number"}, "Grouped Rows", JoinKind.LeftOuter),
#"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Source with Grouped Rows", "Grouped Rows", {"Decision Type List"}, {"Decision Type List"}),
#"Added Include_Exclude" =
Table.AddColumn(
#"Expanded Grouped Rows",
"Include_Exclude2",
each
if List.Count([Decision Type List]) > 1
and List.ContainsAll(
[Decision Type List],
{"Firm Issued", "Firm Finally Rejected", "Reopen Firm Final Reject"}
)
then "Include" else
if List.Count([Decision Type List]) > 1
and List.Count(List.Select([Decision Type List], each _ = "Firm Finally Rejected")) > 1
and List.Contains({"Firm Issued"},[Decision Type List]) = false
then "Exclude" else
if List.Contains(
{"Firm App Recd", "Firm Prelim Rej/ Defcy Ltr", "Reopen Firm Final Reject", "Reopen Firm Prel Rej/ Defcy"},
[Decision Type]
)
then "Exclude" else "Include",
type text
),
#"Removed Columns" = Table.RemoveColumns(#"Added Include_Exclude",{"Decision Type List"})
in
#"Removed Columns"
If that isn't correct, please review your bullet point requirements or make sure your expected result table is accurate.
How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI ReportingThank you for the guidance; however, this is still not correct. The error is within the code below. Here are the correct conditions:
if List.Count([Decision Type List]) > 1 and List.ContainsAll( [Decision Type List], {"Firm Issued", "Firm Finally Rejected", "Reopen Firm Final Reject"} ) then "Include" else if List.Count([Decision Type List]) > 1 and List.Count(List.Select([Decision Type List], each _ = "Firm Finally Rejected")) > 1 and List.Contains({"Firm Issued"},[Decision Type List]) = false then "Exclude" else if List.Contains( {"Firm App Recd", "Firm Prelim Rej/ Defcy Ltr", "Reopen Firm Final Reject", "Reopen Firm Prel Rej/ Defcy"}, [Decision Type] ) then "Exclude" else "Include",
Can you explain how this is Exclude? It does not fit into any of your 3 new critera.
That isn't the only one I am questioning, but I cannot get from your description to the returned values, and I cannot do code if I don't understand the base logic.
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI ReportingPlease see the image below for a justification.
Still not seeing it. The one in yellow is FHA# 08535610. The other one you are pointing to is 08535608. Those aren't the same, so there is no multiple there. All of the 0853* numbers are unique with no repeats.
DAX is for Analysis. Power Query is for Data Modeling
Proud to be a Super User!
MCSA: BI ReportingI apologize, I put in the wrong table data. No wonder you were confused. This is the correct table data.
FHA Number | Property Name | Decision Type | Decision Date | dap_status_id | Sequence | Include_Exclude |
11336007 | A | Firm App Recd | 8/30/2022 | 1478 | 1 | Exclude |
11336007 | A | Firm Prelim Rej/ Defcy Ltr | 9/22/2022 | 1590 | 2 | Exclude |
11336007 | A | Firm Finally Rejected | 12/12/2022 | 1700 | 3 | Include |
08535606 | B | Firm App Recd | 2/11/2022 | 1230 | 1 | Exclude |
08535606 | B | Firm Prelim Rej/ Defcy Ltr | 4/13/2022 | 1234 | 2 | Exclude |
08535606 | B | Firm Finally Rejected | 6/1/2022 | 1320 | 3 | Include |
08535606 | B | Reopen Firm Final Reject | 9/30/2022 | 1450 | 4 | Exclude |
08535606 | B | Firm Finally Rejected | 1/13/2023 | 1555 | 5 | Exclude |
11230077 | C | Firm App Recd | 8/5/2022 | 1279 | 1 | Exclude |
11230077 | C | Firm Prelim Rej/ Defcy Ltr | 9/2/2022 | 1300 | 2 | Exclude |
11230077 | C | Firm Finally Rejected | 9/3/2022 | 1456 | 3 | Include |
11230077 | C | Reopen Firm Final Reject | 9/4/2022 | 1598 | 4 | Exclude |
11230077 | C | Firm Issued | 9/6/2022 | 1609 | 5 | Include |
Given the corrected parameters, how would I adjust the code?
User | Count |
---|---|
8 | |
7 | |
5 | |
5 | |
4 |
User | Count |
---|---|
14 | |
13 | |
8 | |
6 | |
6 |