Forum Discussion
Power Query find minimum/maximum dates across multiple tables
- 10 years ago
Btw, looks like there's a slight confusion in the function signature:
FnMinMaxDate = (TableName as table, ColumnName as text)
Do you want your first parameter to be table, or a table name? Looking at your parameters name and your input table, you're passing down the table's name (text), but your parameter type is table. I would recommend passing the table itself, instead of its name (check Table.Column's input parameters). So it should look like this:
FnMinMaxDate = (InputTable as table, ColumnName as text)
And your input table:
Table
Name(table) ColumnName(text)------------------------ ------------------
Sales SalesDate
Sales OrderDate
Purchase PurchaseDate
Does this make sense?
- 10 years ago
And then, what you need is probably to call Table.AddColumn function (in the query editor, you would see a button "Add Custom Column" for it)
you would call it in a way like this:
= Table.AddColumn(tableWithTablesAndColumnNames, "MinAndMax", each FnMinMaxDate([Table], [ColumnName]))
One other thing, if you're returning only 1 row, consider returning just a record instead of a table with 1 row.
I need to create the input table myself, but I expect it to have to columns. First column should contain the name of the the table and the second column should contain the name of the columns containing the dates in the table. Since there can be mutiple date columns in a fact table I would have to list those table multiple times.
This input table could look like this.
TableName ColumnName
---------------- ------------------
Sales SalesDate
Sales OrderDate
Purchase PurchaseDate
The first output using my function would in this example contain 2 colums and 3 rows
MinDate MaxDate
--------------- ---------------
2014-05-01 2016-04-22 <= being min and max data in Sales.SalesDate
...... ..... <= being min and max date in Sales.OrderDate
...... ...... <= being min and max date in Purchase.PurchaseDate
From this I can then calculate the min year and max year of dates in all my data and dynamically create a dates table that I am sure will contain all the dates present in my data.
I hope this makes sence
Btw, looks like there's a slight confusion in the function signature:
FnMinMaxDate = (TableName as table, ColumnName as text)
Do you want your first parameter to be table, or a table name? Looking at your parameters name and your input table, you're passing down the table's name (text), but your parameter type is table. I would recommend passing the table itself, instead of its name (check Table.Column's input parameters). So it should look like this:
FnMinMaxDate = (InputTable as table, ColumnName as text)
And your input table:
TableName(table) ColumnName(text)
------------------------ ------------------
Sales SalesDate
Sales OrderDate
Purchase PurchaseDate
Does this make sense?
- arify10 years agoMicrosoft Employee
And then, what you need is probably to call Table.AddColumn function (in the query editor, you would see a button "Add Custom Column" for it)
you would call it in a way like this:
= Table.AddColumn(tableWithTablesAndColumnNames, "MinAndMax", each FnMinMaxDate([Table], [ColumnName]))
One other thing, if you're returning only 1 row, consider returning just a record instead of a table with 1 row.
- sdjensen10 years agoSolution Sage
How do I create a table that in the 1st column contains the full table and then column names that I type in the 2nd column?
Is there a function that will return the full table in a single cell?
I would really prefer a solution where i create this in one single query and then loop my custom function instead of having a query with my function and then calling this in another query, but if this works then I will have to live with that.
- arify10 years agoMicrosoft Employee
About how to create a table with tables in it, #table function is an easy way in M
table1 = ..
table2 = ...
table3 = ...
tablesTable = #table({"first column name", "second column name"
] , { {table1, columnname1], {..], {..] ..
(replace ]s with closing curly braces, there's a forum bug that swallows my code apparently :) )
- sdjensen10 years agoSolution Sage
I used Table.FromRecords function to create my table and then call my custom function with values from this table.
Thanks again.
- sdjensen10 years agoSolution Sage
Figured out how to create my input table with tables in the first column... just need to work the rest.. I will get back to you if I need more help and mark as reply if I don't.
I am new to M, but it's really awesome to work with, but also frustrating at times.. ha ha
Thanks mate.
- arify10 years agoMicrosoft Employee
I'm glad you liked M :) If you're interested, there are some resources (like books or blogs) that can help you learn more about it.