Power Query, row by row the sum of the next 3 values - powerquery

I have a power query table, 1 column with integer values. In another column, the sum of the current row and the other 2 rows should be calculated row (cell) by row (cell). - In plain Excel, I calculate it like this:
B1: = SUM(B1:B3)
B2: = SUM(B2:B4)
B3: = SUM(B3:B5)
...
How can I solve this with Power Query? If an error occurs in the last 2 lines, this is negligible.
Thanks and regards
Guenther

Is this what you're looking for?
If you start with this as your Source table:
Then if you add a custom column set up like this:
You'll get this:
Here's the M code, loading it from a spreadsheet's workbook, where the data is in a table named Table1:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Custom", each List.Sum(List.Range(Source[Column1],[Column1]-1,3)))
in
#"Added Custom"

Related

Importing total sum of one column filtering multiple values of one column

I am trying to import the total sum of one column filtered with some conditions. The problem is when I want to add 2 or more conditions to one specific column.
Below my formula:
QUERY(IMPORT RANGE("File","Data Base!A1:R10000"), "Select Sum(Col6) where Col1 = '"&$E$36&"' and Col10 = '"&$E$38&"' and Col17 = '"&$B44&"' label sum(Col6)''")
I want also Column 10 will be equal to E38 and also E39. How can I modify the formula to be able to consider cell E39?

Power Query - Modify tables before combining them

On my table (point1) I am trying to get, that for each table of the grouped rows (point 2) I will have new row inserted (point 3) at the beginning of each table with the value in the column "Metadata1" equal to value form "Column2" for original row number 2 (starting counting from 0).
Link to excel file:
https://filebin.net/cnb4pia0vvkg937g
Its hard to know how much of your requirement is generic or specific, but
TransformFirst = Table.TransformColumns(#"PriorStepName",{{"Count", each
#table(
{"Column1","Column2","Metadata1"},
{{_{0}[Column1],_{0}[Column2],_{2}[Column2]}}
) & _
}}),
Together=Table.Combine(TransformFirst[Count])
in Together
modifies all the tables in column Count to include an extra row that is made up up Row0/Col1 Row0/Col2 and Row2/Col2
It then combines all those tables into one table

PowerQuery filter on 20th of the month no matter the month or year

I am trying to filter a list on the 20th of the month as this has been given as a significant date to identify a specific subset of records. There is no set date just a set day so it can be the 20th of any month in any year. Is there a way I can filter on these in PowerQuery?
Thanks
I assume you mean you want to filter a Table, choosing only to show the rows where the day = the 20th
Let's also also assume your data is loaded into Powerquery, and the date info is a column named Date
Add column, custom column, with formula
= Date.Day([Date])
( See the Power Query M function reference list )
Click at top of that new column and use the drop down filter to [x] the 20
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Custom", each Date.Day([Date])),
#"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Custom] = 20))
in #"Filtered Rows"

Power Query - Merge Pivot Combined Columns into rows

Hi, I'm creating a new thread since the problem I'm trying to solve is different to similar solutions which I've tried unsuccessfully.
I have a table with the following structure (see below), column "City" provides a list of cities A,B...D Column "Date 1" provides dates for the 1st date of an event happening at each city. Column "Date 2" provides the dates for the second event at each city.
City
Date 1
Date 2
A
4/4
5/3
B
4/5
5/4
C
4/6
D
4/7
5/5
I'm trying to bring all the dates for both events into a single column as shown in the example below: Column "Date". While I'm able to pivot columns into rows using Power Query's Split function, I'm unable to solve this specific problem since the data across two separate columns "Date 1" and "Date 2".
Any Power Query ideas to solve this would be awesome, thanks in advance everyone!
City
Date
Date
A
4/4
B
4/5
C
4/6
D
4/7
A
5/3
B
5/4
D
5/5
Right click the City column choose "unpivot other columns"
Then remove extra columns, sort, rename columns as needed
Another fairly quick way to do this in the GUI:
Select the date columns and click Merge Columns (under the Transform tab, Text Column section).
Choose a separator, say Semicolon, and click OK to do the merge.
Now choose Split Column > By Delimiter and choose the delimiter you just used (e.g. Semicolon).
IMPORTANT: Under advanced options, choose Split into Rows and click OK.
Filter out any nulls/blanks from the merged column.
#horseyride's suggestion is certainly fewer steps and cleaner code.
let
Source = <Your Data Source Here>
#"Unpivoted Columns" = Table.Unpivot(Source, {"Date 1", "Date 2"}, "Column", "Date"),
#"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns", {"Column"})
in
#"Removed Columns"

PowerQuery: taking the average of each of many columns

I'm new to PowerQuery and I have a table that is essentially a matrix of dates and hours within those days: the first column holds each date and the rest of the columns are labeled 1 through 24. An example is:
Date H1 H2 H3 H4 ...
---- -- -- -- --
Jan 1
Jan 2
Jan 3
...
This is stored in an Excel file that is quite large, so I want to be able to simply query that file and pull subsets of the data. One example is the average hourly number by year. In SQL this would be represented by "SELECT YEAR(Date), AVG(H1), AVG(H2), ... FROM Source Table GROUPBY YEAR(Date)". However, in PowerQuery it seems like you can only use GROUPBY to generate a new column with the grouped result and thus have to repeat the operation x24 in this case, or more if I had data by seconds for example (to be fair, in the SQL query you also have to type out each column if you don't consider scripting solutions). Is there a simpler approach to generate my desired table (essentially collapsing each column to its average), or do I need to manually add each column?
You can unpivot your hour columns and then you only need to group by year and the unpivoted attribute column.
I made a sample table of your data like this and loaded it into power query. I converted the Date column to Year only, Unpivoted Other Columns on the Date column, then Grouped by the Date and Hour column after unpivoting. The result looks like this.
You can of course repivot the data after if you want inside or outside of power query. This is what the code in power query looks like, but this was all created with normal menu options, not written by hand.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Extracted Year" = Table.TransformColumns(Source,{{"Date", Date.Year, Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Extracted Year", {"Date"}, "Hour", "Value"),
#"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", {"Date", "Hour"}, {{"Average", each List.Average([Value]), type number}})
in
#"Grouped Rows"

Resources