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
bohaihk2022
Regular Visitor

Power Query with looping error

Hi,

I am writting a query with looping, but there is a error message I cannot figure it out what's wrong. Thanks for your help!

 

Below are the queries

 

Order

Order到期日

A1/8/2022
B1/10/2022
C1/12/2022
D1/1/2023
E1/8/2023

 

 

// Test_Query
let
#"List" = {"Order1", "Order2"},
Source = Excel.CurrentWorkbook(){[Name="Order"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"到期日", type date}}),
#"Add Remaining Maturity Tenor Index" = Table.AddColumn(#"Changed Type", "Remaining Maturity Tenor Index", each List.Count(let val = [到期日] in List.Select(Tenor[Date], each _ <=val))-1 ),
#"Records of with Multiple Column" = Table.AddColumn(#"Add Remaining Maturity Tenor Index", "Order Tenor", each Record.Combine({Record.AddField([], List{0}, Table.Column(Tenor, List{0}){[Remaining Maturity Tenor Index]}), Record.AddField([], List{1}, Table.Column(Tenor, List{1}){[Remaining Maturity Tenor Index]})})),
#"Expanded {0}3" = Table.ExpandRecordColumn(#"Records of with Multiple Column", "Order Tenor", {"Order1", "Order2"}, {"Tenor.Order1", "Tenor.Order2"}),
#"New with Looping" = Table.AddColumn(#"Add Remaining Maturity Tenor Index", "OrderTenor", each Record.Combine(List.Generate(() => 0, each _ <= List.Count(#"List")-1, each _ +1, each Record.AddField([], List{_}, Table.Column(Tenor, List{_}){[Remaining Maturity Tenor Index]}))))
in
#"New with Looping"

 

Tenor

DateOrder1Order2

1/1/1900OverdueNext day or less
1/8/2022Next dayNext day or less
2/8/20222-7 days2-7 days
2/8/20222-7 days2-7 days
2/8/20222-7 days2-7 days
2/8/20222-7 days2-7 days
2/8/20222-7 days2-7 days
2/8/20222-7 days2-7 days
2/8/20222-7 days2-7 days
3/8/20222-7 days2-7 days
8/8/2022Between 8 days and 1 month8 days to 1 month
9/8/2022Between 8 days and 1 month8 days to 1 month
1/9/2022Between 1 and 3 months1 to 3 months
1/11/2022Between 3 months and 1 year3 to 6 months
1/2/2023Between 3 months and 1 year6 to 12 months
1/8/2023Later than 1 year1 to 2 years
1/8/2024Later than 1 year2 to 3 years
1/8/2025Later than 1 year3 to 4 years
1/8/2026Later than 1 year4 to 5 years
1/8/2027Later than 1 year5 to 7 years
1/8/2029Later than 1 year7 to 10 years
1/8/2032Later than 1 year10 to 15 years
1/8/2037Later than 1 year15 to 20 years
1/8/2042Later than 1 yearMore than 20 years

 

// Tenor
let
Source = Excel.CurrentWorkbook(){[Name="Tenor"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}})
in
#"Changed Type"

1 ACCEPTED SOLUTION
wdx223_Daniel
Super User
Super User

= let a =Table.Buffer(Table.Sort(Tenor,"Date")) in Table.FromRecords(List.Transform(Table.ToRecords(#"Changed Type"),each _&Record.SelectFields(Table.Last(Table.RemoveLastN(a,(x)=>x[Date]>[到期日])),#"List")))

View solution in original post

4 REPLIES 4
wdx223_Daniel
Super User
Super User

= let a =Table.Buffer(Table.Sort(Tenor,"Date")) in Table.FromRecords(List.Transform(Table.ToRecords(#"Changed Type"),each _&Record.SelectFields(Table.Last(Table.RemoveLastN(a,(x)=>x[Date]>[到期日])),#"List")))

It works! Many thanks! Could you please also share with me the meaning in each step? Thanks!

Anonymous
Not applicable

What is the error message you saw? Sharing it can help others understand easier.

it is in chinese, 

"Expression.Error: 無法對類型Number應用字段訪問。

詳細信息:

Value=0

Key=Remaining Maturity Tenor Index

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

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 community update carousel

Fabric Community Update - June 2025

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

Top Solution Authors