Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Hello. I have table1 and table2 (reference book). To table 1 it is necessary to put a column " value" from Table 2, if Т1date is in an interval between dates in Т2 and numbers correspond
T1
T2
Result
Sorry for my english, I write through translate.google.com
Solved! Go to Solution.
My suggestion would be to expand Table2 so you will have a value for each date and you can just merge both tables directly on keys "date" and "number".
This can be done quite easily by importing the table with ALL number type fields (also the date fields!!), add a custom column with lists of values that represent all dates from "from" to "to" (both inclusive), expand this list column, adjust the data type to date and select/reorder only the required columns.
let Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,{{"from", type number}, {"to", type number}, {"number", type number}, {"value", type number}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "date", each {[from]..[to]}), #"Expanded date" = Table.ExpandListColumn(#"Added Custom", "date"), #"Changed Type1" = Table.TransformColumnTypes(#"Expanded date",{{"date", type date}}), #"Removed Other Columns" = Table.SelectColumns(#"Changed Type1",{"date", "number", "value"}) in #"Removed Other Columns"
@mechanix85 struggling a little bit to understand what you are saying
might help if you show what your expected results are
Proud to be a Super User!
if the column "date" from T1 is in the interval of the dates T2 (from to) and the column "number" from T1 = column "number" from T2, then it will get the value from the column "value" T2
in excel formula
=SUMPRODUCT((table2[from]<=table1[@date])*(table2[to]>=table1[@date])*(table2[number]=table1[@number])*table2[value])
@mechanix85 now i get it 🙂
what about a calculated column
test =
CALCULATE (
VALUES ( number ),
FILTER ( T2, T1[date] >= T2[from] && T1[date] <= T2[to] )
)
Proud to be a Super User!
Thanks, but I know how to get the result using DAX, I would like to do this with the help of a power query
@mechanix85 ah i see, ok i thought you said you wanted to improve performance, maybe someone like @MarcelBeug can help.
Proud to be a Super User!
My suggestion would be to expand Table2 so you will have a value for each date and you can just merge both tables directly on keys "date" and "number".
This can be done quite easily by importing the table with ALL number type fields (also the date fields!!), add a custom column with lists of values that represent all dates from "from" to "to" (both inclusive), expand this list column, adjust the data type to date and select/reorder only the required columns.
let Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,{{"from", type number}, {"to", type number}, {"number", type number}, {"value", type number}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "date", each {[from]..[to]}), #"Expanded date" = Table.ExpandListColumn(#"Added Custom", "date"), #"Changed Type1" = Table.TransformColumnTypes(#"Expanded date",{{"date", type date}}), #"Removed Other Columns" = Table.SelectColumns(#"Changed Type1",{"date", "number", "value"}) in #"Removed Other Columns"
@MarcelBeug, Thanks, I wrote above the code above. We shall stop on this variant
let source = Excel.CurrentWorkbook(){[Name="table2"]}[Content], #"ch type" = Table.TransformColumnTypes(source,{{"from", type date}, {"to", type date}, {"number", type number}, {"value", type number}}), #"dates" = Table.AddColumn(#"ch type", "date", each List.Dates([from],Number.From([to])-Number.From([from])+1,#duration(1,0,0,0))), #"dates Expand" = Table.ExpandListColumn(#"dates", "date"), #"ch type2" = Table.TransformColumnTypes(#"dates Expand",{{"date", type date}}) in #"ch type2"
As an option, I made a column of dates from Table 2 using List.
And then you can merge two tables
But I think there should be a more refined move, because My table of 1600 lines turned out already at 200,000
let source = Excel.CurrentWorkbook(){[Name="table2"]}[Content], #"ch type" = Table.TransformColumnTypes(source,{{"from", type date}, {"to", type date}, {"number", type number}, {"value", type number}}), #"dates" = Table.AddColumn(#"ch type", "date", each List.Dates([from],Number.From([to])-Number.From([from])+1,#duration(1,0,0,0))), #"dates Expand" = Table.ExpandListColumn(#"dates", "date"), #"ch type2" = Table.TransformColumnTypes(#"dates Expand",{{"date", type date}}) in #"ch type2"
I created (in the first in my life) function for table2
/*query name=
fn_table2*/
(date as date, cod as number) => let source = Excel.CurrentWorkbook(){[Name="table2"]}[Content], #"Changed type" = Table.TransformColumnTypes(source,{{"from", type date}, {"to", type date}, {"number", type number}, {"value", type number}}), #"Strings with applied filter" = Table.SelectRows(#"Changed type", each [from] <= date and [to] >= date and [number]=cod) in #"Strings with applied filter"
And then called it to request a table1
/*query name=
table1*/
let source = Excel.CurrentWorkbook(){[Name="table1"]}[Content], #"Changed type" = Table.TransformColumnTypes(source,{{"date", type date}, {"number", type number}}), #"fn" = Table.AddColumn(#"Changed type", "Custom", each try fn_table2([date], [number]) otherwise null), #"AddCustom" = Table.ExpandTableColumn(#"fn", "Custom", {"value"}, {"Custom.value"}) in #"AddCustom"
This all works, however, when I started the query on my database (table1 = 100000 rows, table2 = 1600), the query works very long.
Are there options to speed up the work?
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
111 | |
94 | |
83 | |
67 | |
59 |
User | Count |
---|---|
151 | |
121 | |
104 | |
87 | |
67 |