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
PBInewbie17
Helper I
Helper I

Add column without circular reference warning

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:

  • If [Decision Type] = "Firm App Recd", "Firm Prelim Rej/ Defcy Ltr", "Reopen Firm Final Reject", or "Reopen Firm Prel Rej/ Defcy" then “Include_Exclude” should equal "Exclude".
  • If a duplicate FHA Number has a Decision Type of “Firm Issued” and a Decision Type of “Firm Finally Rejected”, then every single Decision Type associated with that FHA Number should equal “Include” in the “Include_Exclude” column.
  • If a duplicate FHA Number has more than one “Firm Finally Rejected” Decision Type but has no Decision Type of “Firm Issued”, then the maximum “Sequence” of the “Firm Finally Rejected” should equal “Exclude” in the “Include_Exclude” column. Everything else for that same FHA Number can equal “Include”.

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 NumberProperty NameDecision TypeDecision Datedap_status_idSequenceInclude_Exclude
11336007AFirm App Recd8/30/202214781Exclude
11336007AFirm Prelim Rej/ Defcy Ltr9/22/202215902Exclude
11336007AFirm Finally Rejected12/12/202217003Include
08535606BFirm App Recd2/11/202212301Exclude
08535607BFirm Prelim Rej/ Defcy Ltr4/13/202212342Exclude
08535608BFirm Finally Rejected6/1/202213203Include
08535609BReopen Firm Final Reject9/30/202214504Exclude
08535610BFirm Finally Rejected1/13/202315555Exclude
11230077CFirm App Recd8/5/202212791Exclude
11230077CFirm Prelim Rej/ Defcy Ltr9/2/202213002Exclude
11230077CFirm Finally Rejected9/3/202214563Include
11230077CReopen Firm Final Reject9/4/202215984Exclude
11230077CFirm Issued9/6/202216095Include
7 REPLIES 7
edhans
Super User
Super User

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.

 

edhans_0-1678757446343.png

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.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Thank you for the guidance; however, this is still not correct. The error is within the code below. Here are the correct conditions:

  • If [Decision Type] = "Firm App Recd", "Firm Prelim Rej/ Defcy Ltr", "Reopen Firm Final Reject", or "Reopen Firm Prel Rej/ Defcy", then record should equal "Exclude".
  • If [Decision Type] contains both "Firm Issued" and "Firm Finally Rejected" for the same [FHA Number], then the formula should return "Include" for both items.
  • If [Decision Type] contains multiple "Firm Finally Rejected" items and does not contain "Firm Issued", then the maximum [Sequence] matching the "Firm Finally Rejected" should equal "Exclude". The other "Firm Finally Rejected" record that is not the maxium should equal "Include".
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.

  1. It isn't in your first list.
  2. It isn't in 2 & 3 either because those involve multiple values, and this is a unique FHA.

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.

edhans_0-1678815875773.png

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Please see the image below for a justification.

  • If [Decision Type] contains multiple "Firm Finally Rejected" items and does not contain "Firm Issued", then the maximum [Sequence] matching the "Firm Finally Rejected" should equal "Exclude". The other "Firm Finally Rejected" record that is not the maxium should equal "Include".

PBInewbie17_0-1678819795359.png

 

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.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

I 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? 

  • If [Decision Type] = "Firm App Recd", "Firm Prelim Rej/ Defcy Ltr", "Reopen Firm Final Reject", or "Reopen Firm Prel Rej/ Defcy", then record should equal "Exclude".
  • If [Decision Type] contains both "Firm Issued" and "Firm Finally Rejected" for the same [FHA Number], then the formula should return "Include" for both items.
  • If a duplicate FHA Number has multiple "Firm Finally Rejected" Decision Types and also has no "Firm Issued" Decision Type, then the maximum [Sequence] associated with the "Firm Finally Rejected" should equal "Exclude". The other "Firm Finally Rejected" record that is not the maximum should equal "Include".

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.