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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
adentler
Advocate I
Advocate I

Power Query Add Custom Column with multiple criteria lookup from other table

 

Hello, I am trying to use Power Query to add a column to a table during the transform process. I need to lookup multiple values from Table 1 (Finance Book) in Table 2 (AC_CC Mapping), and return the Contract Year as the column.

 

Finance Book (sample set)

Document Number

Cost Center

Posting Date

12345

1234

8/5/2024

67890

5678

7/2/2025

 

AC_CC Mapping (sample set)

Account Code

Contract Year

Start Date

End Date

Cost Center

123AB

Year 1

7/1/2024

6/30/2025

1234

123AB

Year 1

7/1/2024

6/30/2025

5678

223AB

Year 2

7/1/2025

6/30/2026

5678

 

Pseudo:

Look up FinanceBook.Cost Center in AC_CC Mapping.Cost Center

Where FinanceBook.Posting Date >= AC_CC Mapping.Start Date

  and <=AC_CC Mapping. End Date,

Return AC_CC Mapping.Contract Year

 

Finance Book

Document Number

Cost Center

Posting Date

Contract Year

12345

1234

8/5/2024

Year 1

67890

5678

7/2/2025

Year 2

31649

5678

9/3/2024

Year 1

 

I have tried adding a Custom Column during the transform process using Power Query with the following Code but am receiving an error:

 

#"AC_CC Mapping"[Contract Year]{List.PositionOf(#"AC_CC Mapping "[Cost Center], [Cost Center] and ([Posting Date] >= #"AC_CC Mapping "["[Start Date] and [Posting Date] <= "AC_CC Mapping "[ [End Date]))}

 

Error: Expression.Error: We cannot convert the value "5678" to type Logical.

Details:

    Value=5678

    Type=[Type]

 

Any ideas?

1 ACCEPTED SOLUTION
adentler
Advocate I
Advocate I

All - I believe I was able to determine the solution thanks to all of your inputs. I also found the cyclical reference. It was a calculated column post-transform on the Finance Book table.

 

Ultimately, I was able to use the following code and the column was added as needed:

 

= Table.AddColumn(#"Changed Type", "Contract Year", (F) => 
  Table.SelectRows( 
    #"ACC_CC Mapping", 
    (C) => C[Start Date] <= F[posting_date] and C[End Date] >= F[posting_date] 
  )
)

 

 

View solution in original post

11 REPLIES 11
adentler
Advocate I
Advocate I

All - I believe I was able to determine the solution thanks to all of your inputs. I also found the cyclical reference. It was a calculated column post-transform on the Finance Book table.

 

Ultimately, I was able to use the following code and the column was added as needed:

 

= Table.AddColumn(#"Changed Type", "Contract Year", (F) => 
  Table.SelectRows( 
    #"ACC_CC Mapping", 
    (C) => C[Start Date] <= F[posting_date] and C[End Date] >= F[posting_date] 
  )
)

 

 

v-kpoloju-msft
Community Support
Community Support

Hi @adentler,

Thank you for reaching out to the Microsoft Fabric Community Forum. Also, thanks to @SundarRaj@MarkLaf@AmiraBedh, for those inputs on this thread.

Has your issue been resolved? If the response provided by the community member @SundarRaj, @MarkLaf, addressed your query, could you please confirm? It helps us ensure that the solutions provided are effective and beneficial for everyone.

Hope this helps clarify things and let me know what you find after giving these steps a try happy to help you investigate this further.

Thank you for using the Microsoft Community Forum.

I am still working through the solutions provided and am happy to update this post once I have. I sencerely appreciate the time and effort @SundarRaj@MarkLaf@AmiraBedhhave provided to help me resolve me issue. 

Hi @adentler,

Just checking in to see if the issue has been resolved on your end. If the earlier suggestions helped, that’s great to hear! And if you’re still facing challenges, feel free to share more details happy to assist further.

Thank you.

This has been resolved. Thank you! Everyones suggestions and solutions did help. I continued to run into the cyclical value but when I ultimately resolved it, updated my SQL query, and managed my relationships better.. I overcame the issue. 

SundarRaj
Super User
Super User

Hi @adentler, another PQ solution. Let me know if this is what you were looking for. I have indeed used List.PositionOf here. I think one important  parameter you missed here in List.PositionOf was Occurence.All

It would give you all the rows associated with that cost center and post that, adding a condition custom column would give you boolean values of what you desire.

SundarRaj_0-1757144374563.png


I'll attach the code below. Let me know if you need the file for better understanding of the steps. Thanks

let
// Finance Book Table
FinanceBook = #table(
{"Document Number", "Cost Center", "Posting Date"},
{{12345, 1234, #date(2024, 8, 5)}, {67890, 5678, #date(2025, 7, 2)}}
),
// AC_CC Mapping Table
AC_CC_Mapping = #table(
{"Account Code", "Contract Year", "Start Date", "End Date", "Cost Center"},
{
{"123AB", "Year 1", #date(2024, 7, 1), #date(2025, 6, 30), 1234},
{"123AB", "Year 1", #date(2024, 7, 1), #date(2025, 6, 30), 5678},
{"223AB", "Year 2", #date(2025, 7, 1), #date(2026, 6, 30), 5678}
}
),
AddColumn = Table.AddColumn(
FinanceBook,
"Values",
each List.Transform(
List.PositionOf(AC_CC_Mapping[Cost Center], [Cost Center], Occurrence.All),
each AC_CC_Mapping{_}
)
),
List = Table.ExpandListColumn(AddColumn, "Values"),
Record = Table.ExpandRecordColumn(
List,
"Values",
{"Account Code", "Contract Year", "Start Date", "End Date", "Cost Center"},
{"Account Code", "Contract Year", "Start Date", "End Date", "AC_CC_Mapping.Cost Center"}
),
#"Added Custom" = Table.AddColumn(
Record,
"Condition",
each [Posting Date] >= [Start Date] and [Posting Date] <= [End Date]
),
#"Filtered Rows" = Table.RemoveColumns(
Table.SelectRows(#"Added Custom", each ([Condition] = true)),
"Condition"
)
in
#"Filtered Rows"

Regards,

Sundar Rajagopalan

Thank you! Would you please provide to me the file? I am running into missing token erros here - which I am 100% is all me!

 

Also - it appears in the code you placed in your comment that you are creating the table as I posted it. Am I correct? If so, I don't believe this will work as the tables already exist and are larger that the sample set I provided. Table 1 has a SQL data source and Table 2 is Excel. 

MarkLaf
Super User
Super User

You'll usually get better performance with a join in this kind of scenario. In case you somehow added a dependency on 'Finance Book' within 'AC_CC Mapping', I've written this as a third query, which should avoid circular ref errors.

 

// Assuming that we'll rename original 'Finance Book' as 'Finance Book_init' and disable load
// We can then rename this new query as 'Finance Book' to load into model
let
    // Get dates from start - end on map and expand
    Map = #"AC_CC Mapping",
    Map_AddDates = Table.AddColumn(
        Map, "Dates", 
        each List.Dates( 
            [Start Date], 
            Int64.From( [End Date] - [Start Date] ) + 1, 
            #duration(1,0,0,0) 
        ),
        type {date}
    ),
    Map_ExpandDates = Table.ExpandListColumn(Map_AddDates, "Dates"),

    // join map and expand Contract Year on above Map_ExpandDates
    Book = #"Finance Book_init",
    Book_JoinMap = Table.NestedJoin( 
        Book, {"Cost Center", "Posting Date"}, 
        Map_ExpandDates, {"Cost Center", "Dates"}, 
        "join", JoinKind.LeftOuter 
    ),
    Book_ExpandContractYear = Table.ExpandTableColumn(
        Book_JoinMap, "join", 
        {"Contract Year"}, {"Contract Year"}
    )
in
    Book_ExpandContractYear

 

MarkLaf_0-1757102587121.png

 

----------

As an aside, a circular reference error from the AmiraBedh's solution seems odd. If there was a circular reference issue at the time of your original post, I believe it would have taken priority over your type mismatch error (as in, you would not have seen the error you posted, only the circular ref error). Did you change something in your queries (that would have introduced a Finance Book -> AC_CC Mapping dependency) between your original post and when you tried solutions? Or did you paste the code (which references FinanceBook) inside the FinanceBook query? If a query references itself, that would also cause the error.

Thank you MarkLaf. Even with your solution I am getting the Cyclic error. There was not a cyclic error occurring at the time of my intitial post. Originally, my FinanceBook table had a calculated column in it that referenced the Account mapping table. This occurred after the transform process. However, when I received the cyclic reference after applying AmiraBedh's solution, I removed the tables completly from the file then re-added them without any transformations, custom columns etc to ensure clean, un-changed, data.

 

I did follow-these steps with your code:

Open Power Query -> Select FinanceBook -> Add Column -> Customer Column -> Paste code -> Ok.. result: Cyclic error.

 

 

Table 1 is imported using a SQL query where as Table 2 is an excel spreadsheet. I have no doubt I am doing something wrong here and truly appreciate yours and everyones help on this.  Ive been thrown in the fire a little green.

AmiraBedh
Super User
Super User

Hello !

Thank you for posting on Microsoft Fabric community.

The AND only works on logical values but I can see that in your code you are mixing a value ofCost Center with boolean.  I shared the file with the solution.

 

= let
    FB  = Table.TransformColumnTypes(FinanceBook,     {{"Cost Center", type text}, {"Posting Date", type date}}),
    MAP = Table.TransformColumnTypes(#"AC_CC Mapping",{{"Cost Center", type text}, {"Start Date", type date}, {"End Date", type date}}),

    WithCY = Table.AddColumn(
        FB, 
        "Contract Year", 
        (row as record) as nullable text =>
            let
                matches = Table.SelectRows(
                    MAP, 
                    each [Cost Center] = row[Cost Center] 
                      and row[Posting Date] >= [Start Date] 
                      and row[Posting Date] <= [End Date]
                ),
                best = if Table.IsEmpty(matches) 
                       then null 
                       else Table.Sort(matches, {{"Start Date", Order.Descending}}){0}[Contract Year]
            in
                best,
        type nullable text
    )
in
    WithCY

 

AmiraBedh_0-1757009622184.png

 


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

Thank you! I truly appreciate your response and can definitely see the what you did in the code. However, I am still getting an error, but a different one.. this time the error is: 

 

Expression.Error: A cyclic reference was encountered during evaluation.

 

I used your exact code with great hope! I am pretty new to PBI.. and Power Query so thank you for helping me work through it.

Helpful resources

Announcements
October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors