Power Query MAX Value from a table - powerquery

I trying to get the MAX date from a table onto a different date with power query. At the moment I'm stuck and all I get is a table based on a condition. Not sure if this is clear, so I'll explain with the code. This is my code at the moment:
let
Source = Table.NestedJoin(Table.NestedJoin(SKU,{"SKU"},q_UltColh_NEW,{"SKU"},"qUltColh_NEW",JoinKind.LeftOuter),{"SKU"},r_STK,{"SKU"},"Rep_Stk", JoinKind.LeftOuter),
.
.
.
#"Expanded Origem" = ...
#"Expanded Origem" = Table.ExpandTableColumn(#"Merged Queries", "Origem", {"Desc_ORI", "Parent_ORI"}, {"Origem.Desc_ORI", "Origem.Parent_ORI"}),
#"Added Last_Rec" = Table.AddColumn(#"Expanded Origem", "Last_Rec", each
let SKU = [SKU]
in Table.SelectRows(r_GOODSREC,each [SKU]=SKU)
)
in
#"Added Last_Rec"
I have two tables:
SKU Desc
46_24_ ABC
103_5_ DEF
doc_DATE RowNo SKU Cod_ART QTT
10/01/2017 1 46_24_ 46.24 50
14/01/2017 1 46_24_ 46.24 100
14/01/2017 1 103_5_ 103.5 50
16/01/2017 1 103_5_ 103.5 100
And I want to get:
SKU Desc Last_Entry Qtt
46_24_ ABC 14/01/2017 50
103_5_ DEF 16/01/2017 100
my code is returning a table with various columns:
SKU Desc Last_Entry
46_24_ ABC Table
103_5_ DEF Table
I believe once I get the max value I can just exapand the table, unless you tell me that is a bad ideia.
Thank you very much,

I got this with the code below:
Notes:
1. My date format is month/day/year whereas yours was day/month/year.
2. Also, in your question, you show an expected QTT of 50 for SKU 46_24_; but your source table has 100 as the QTT for the latest date of SKU 46_24_, which is why my table has 100 instead of 50.
I used the same two starting tables as you. I called them Table1 and Table2. (Table1 is the one with just the SKU and Desc columns.)
Then I merged those two tables into a new table called Merge1, using a left-outer join.
I guess the key points are:
I used "Group By" (i.e., Table.Group) to group by each SKU and get its max date value in a column I called Last_Entry, and I included all row data in a column I called AllData. Here's the Group By pop-up window:
Then, after the Group By, I expanded the embedded table in AllData and added a new column to flag and filter out the rows where the doc_Date was not equal to Last_Entry.
let
Source = Table.NestedJoin(Table1,{"SKU"},Table2,{"SKU"},"Table2",JoinKind.LeftOuter),
#"Expanded Table2" = Table.ExpandTableColumn(Source, "Table2", {"doc_Date", "RowNo", "SKU", "Cod_ART", "QTT"}, {"doc_Date", "RowNo", "SKU.1", "Cod_ART", "QTT"}),
#"Changed Type" = Table.TransformColumnTypes(#"Expanded Table2",{{"doc_Date", type date}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"SKU"}, {{"Last_Entry", each List.Max([doc_Date]), type datetime}, {"AllData", each _, type table}}),
#"Expanded AllData1" = Table.ExpandTableColumn(#"Grouped Rows", "AllData", {"Desc", "doc_Date", "QTT"}, {"Desc", "doc_Date", "QTT"}),
#"Added Custom" = Table.AddColumn(#"Expanded AllData1", "Custom", each if[Last_Entry]=[doc_Date] then "Last_Entry" else "NotLast_Entry"),
#"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Custom] = "Last_Entry")),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Custom", "doc_Date"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"SKU", "Desc", "Last_Entry", "QTT"})
in
#"Reordered Columns"

Related

PowerQuery / multiply many columns with a single column / shift up results without blank(null)

First of all, sorry for my broken English. If something might feel offence, that is my fault. Sorry (and Thanks) in advance.
As title said, I want to get a way how to multiply many rows with one single row in one step: regardless how many columns and whatever column name is, & ignore blank cells then shift up filled results in POWERQUERY.
want to get a final table in yellow, with merging Ratio table and Total table.
want to know how to multiply Ratio table (many columns) and Total table (a single column) in one step.
From Grey table, want to get a way how to remove null value and shift up filled results.
prefer "Transform" than "Add column".
if you need an example file, can get it via this link : https://www.dropbox.com/s/fs2cymeak1f2w57/powerquery%20practice.xlsx?dl=0
Im 99% certain Im doing homework for someone, but anyway
Unpivot Ratio. Merge in Total, multiply with custom column. Group on Attribute and add index. Pivot
let Source = Ratio,
// I am too lazy to do my own homework and should be ashamed
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"M"}, "Attribute", "Value"),
#"Filtered Rows1" = Table.SelectRows(#"Unpivoted Other Columns", each ([Value] <> 0)),
#"Merged Queries" = Table.NestedJoin(#"Filtered Rows1", {"M"}, Total, {"Month"}, "Total", JoinKind.LeftOuter),
#"Expanded Total" = Table.ExpandTableColumn(#"Merged Queries", "Total", {"Total"}, {"Total"}),
#"Added Custom" = Table.AddColumn(#"Expanded Total", "Custom", each if [Total]=null then 0 else [Total]*[Value]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Value", "Total","M"}),
#"Grouped Rows" = Table.Group(#"Removed Columns", {"Attribute"}, {{"data", each Table.AddIndexColumn(_, "Month", 1, 1, Int64.Type), type table }}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", { "Custom", "Month"}, { "Custom", "Month"}),
#"Pivoted Column" = Table.Pivot(#"Expanded data", List.Distinct(#"Expanded data"[Attribute]), "Attribute", "Custom")
in #"Pivoted Column"

calculate differences between 2 smallests date for each ID

I have some users with different game_id.
for each user, I want to find differences between the 2 smallest different dates and write the result in another column.
in this example,
for user_id = 1, it should be 4 (difference between 13/2/2001 and 17/2/2001)
for user_id = 2, we don't have any result because we have just 1 date,
for user_id = 3, it should be 3
and for user_id = 4, it should be 3. (difference between 5/10/2003 and 8/10/2003)
How can I calculate it in Power BI?
This does the trick with M code if you feel like using
It groups on user_id, gets the 2 lowest dates from the unique values, and calculates the difference. It then merges back on original data into a new column
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"account_charging_dates", type date}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"user_id"}, {{"Diff", each try Number.From( List.MinN(List.Distinct([account_charging_dates]),2){1} - List.MinN(List.Distinct([account_charging_dates]),2){0}) otherwise null}}),
#"Merged Queries" = Table.NestedJoin(#"Changed Type",{"user_id"},#"Grouped Rows",{"user_id"},"Table1",JoinKind.LeftOuter),
#"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries", "Table1", {"Diff"}, {"Diff"})
in #"Expanded Table1"

Power Query: Duplicate Rows Based on Value

I have a column that contains the Total Stock of an item. I'd like to expand this out into 1 row per item (i.e. the item has 6 in stock and therefore appears as 6 line items).
Is this possible with power query?
The M-Code below will expand this input table
to this
let
Source = Excel.CurrentWorkbook(){[Name="tblData"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ColA", type text}, {"Stock", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Col", each List.Repeat({[ColA]},[Stock])),
#"Expanded Col" = Table.ExpandListColumn(#"Added Custom", "Col")
in
#"Expanded Col"

Keeping the last row in a month and year for a particular group

I have a table which contains roughly 30 columns. One of the columns is "Date" and one is "Project Code". For each project code, there could be multiple entries in the table for a given month and year. For example, for project code "ABC" there could be multiple entries for February, 2020. Each would have a different date (i.e, 20200202, 20200209, 20200216, 20200223, 20200229)
In Power Query, I'd like to perform a transformation step that includes only those rows that represent the last entry for a month and year, for each project code. Using the example above, I want a single row for project "ABC", for February, 2020, and it would be for the date of 20200229.
I immediately looked at Group By but I don't think I'm going to be able to do what I want using Group By.
You need to do something along these lines
Assuming column Date is formatted as a date then ...
Add a column to pull out the month using
= Date.Month([Date])
Add a column to pull out the year using
= Date.Year([Date])
Add a column that finds the maximum date from all the rows with matching (Group, Month, Year)
= Table.AddColumn(#"PriorStep","MaxDate",(i)=>List.Max(Table.SelectRows(#"PriorStep", each [Project Code]=i[Project Code] and [Month]=i[Month] and [Year]=i[Year]) [Date]), type date )
Add a column to compare current date to that maximum date
=if [Date]=[MaxDate] then "keep" else "remove"
Remove the rows that don't match by filtering
Remove the extra columns
Sample code below
let Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Project Code", type text}, {"Date", type date}}),
#"Added Custom2" = Table.AddColumn(#"Changed Type", "Month", each Date.Month([Date])),
#"Added Custom3" = Table.AddColumn(#"Added Custom2", "Year", each Date.Year([Date])),
#"Added Custom" = Table.AddColumn(#"Added Custom3","MaxDate",(i)=>List.Max(Table.SelectRows(#"Added Custom3", each [Project Code]=i[Project Code] and [Month]=i[Month] and [Year]=i[Year]) [Date]), type date ),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Match", each if [Date]=[MaxDate] then "keep" else "remove"),
#"Filtered Rows" = Table.SelectRows(#"Added Custom1", each ([Match] = "keep")),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"MaxDate", "Match","Month","Year"})
in #"Removed Columns"
With Table.Group:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TcwxCsAwDEPRu3jOIMkxScf2GiH3v0Y9uDRgLY+P1zJByJM1u3MEbLdiEsVRyAvS/Nr4U2mUej8eOEvVzzZSntz0Q8FSDtv7BQ==", BinaryEncoding.Base64), Compression.Deflate)), {"Date","Project","SomeData"}),
types = Table.TransformColumnTypes(Source,{{"Date", Int64.Type}, {"Project", type text}, {"SomeData", Int64.Type}}),
year = Table.AddColumn(types, "year", each Number.RoundDown([Date]/10000)),
month = Table.AddColumn(year, "month", each Number.RoundDown(Number.Mod([Date],10000)/100)),
day = Table.AddColumn(month, "day", each Number.Mod([Date],100)),
group = Table.Group(day, {"Project", "year", "month"}, {"temp", each Table.FirstN(Table.Sort(_,{"day",1}),1)}),
combine = Table.Combine(group[temp])
in
combine
Initial data:
Result:

Power Query M - Custom Column for Rolling 28 Days Sales

I'm looking for some Power Query help. I have a huge set of sales data for 40k products over one year. For each product on each day I need to add a 28 day sales column.
I essentially want to do a sumifs like the below but in M.
=SUMIFS([SALES],[Product Code],[This Product Code],[Date],<=[This Date],[Date],>=[This Date]-28))
Try this then, it should work but would likely do so at a crawl
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Sales", Int64.Type}, {"Product Code", type text}, {"Date", type date}}),
TotalAmountAdded = Table.AddColumn(Source, "Total Amount", (i) => List.Sum(Table.SelectRows(Source, each ([Product Code] = i[Product Code] and [Date]<=i[Date] and [Date]>=Date.AddDays(i[Date],-28)))[Sales]), type number )
in TotalAmountAdded
Add a custom column with date logic (based on your sample sumif formula), filter the new column to get the relevant rows, then group by product code and sum Sales. Assuming source data is in Table1 with three columns (Sales,Product Code, Date) the code would be
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Sales", Int64.Type}, {"Product Code", type text}, {"Date", type date}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "AddMe", each if [Date]<=DateTime.Date(DateTime.LocalNow()) and [Date]>=Date.AddDays(DateTime.Date(DateTime.LocalNow()),-28) then 1 else 0),
#"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([AddMe] = 1)),
#"Grouped Rows" = Table.Group(#"Filtered Rows", {"Product Code"}, {{"ProductSales", each List.Sum([Sales]), type number}})
in #"Grouped Rows"

Resources