I have to sort month name from January to December in Power BI Desktop.
Power BI sorts the column according to the alphabetical order.
Here is my dataset:
When I am using sort by column by month number, I am getting an error of circular dependence:
There is no date table in my dataset. I have calculated month number as calculated column.
How can I get rid of this circular dependency? This is the desired result:
The sort column doesn't work if created like this:
It works if created like this:
Go to the power query editor and duplicate the Month column and then create a new step using the following code:
= let
Source = #"Previous Step Name",
Substitutions = [
#"January" = "1",
#"February" = "2",
#"March" = "3",
#"April" = "4",
#"May" = "5",
#"June" = "6",
#"July" = "7",
#"August" = "8",
#"September" = "9",
#"October" = "10",
#"November" = "11",
#"December" = "12"],
Substituted = Table.TransformColumns(Source, {{"Duplicate Month Column", each Record.FieldOrDefault(Substitutions, _, _)}})
in
Substituted
Remember to replace the Previous Step Name and Duplicate Month Column for the corresponding name.
Finally go to the Data tab and select your month column and select the Sort by Column option and sort it using the new created column.
The fastest solution is to create a column in the same table with an IF or SWITCH statement for each month and sort your MonthName by your MonthNumber.
Code below:
Month_Num = SWITCH(TRUE,
[Month] = "Jan",
1,
[Month] = "Feb",
2,
[Month] = "Mar",
3,
[Month] = "Apr",
4,
[Month] = "May",
5,
[Month] = "Jun",
6,
[Month] = "Jul",
7,
[Month] = "Aug",
8,
[Month] = "Sep",
9,
[Month] = "Oct",
10,
[Month] = "Nov",
11,
[Month] = "Dec",
12)
Create a support table for month name and month number .
That support table will be static and it has Month name and month number(better create in excel and copy it into power bi instead creating a calculated column)
In my case my it will be based on fiscal year.
[
Then Sort that support table month name with month number.
Join the month name of support table with month name of your main table .
Drag the month column from support table instead of main table into your Graph.
Thats it.
If its still not sorted then click on graph and sort it based on month name.
Go to Data tab in power bi, Select the column that you need to select and click on the Sort by Column and select the column need to be used for sorting (see below pic, In this case month name is being sorted based on the Month of the Year column)
Related
I need to group a table based on the date and the hourly interval, using the Sum:
Date
Interval: from 8am today to <8am today+1
Previously I was using MS Access and a query to create it. Now I need to go through Power Query in MS Excel.
That was the SQL Query used before:
SELECT switch(Tbl_Prod_Chat.[Interval]>=8,Tbl_Prod_Chat.[Date],Tbl_Prod_Chat.[Interval]<8,Tbl_Prod_Chat.[Date]-1) AS LINK_DATE, Tbl_Prod_Chat.Agent, Sum(Tbl_Prod_Chat.ProdChat) AS Prod_Chat
FROM Tbl_Prod_Chat
GROUP BY Switch(Tbl_Prod_Chat.[Interval]>=8,Tbl_Prod_Chat.[Date],Tbl_Prod_Chat.[Interval]<8,Tbl_Prod_Chat.[Date]-1), Tbl_Prod_Chat.Agent;
The table is built as:
Field 1 "Date" (type/format: mm/dd/yyyy)
Field 2 "Interval" (type: whole number): 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0
Field 3 "Volume of contact" (type: whole number)
The new table would be:
Field 1 "Date"
Field 2 "Total Volume" (sum on 24h from 8am toady to <8am Today+1).
Can you please help me on this?
Thanks
Seb
Sounds like you just need to add a single custom column
add column .. custom column...
= if [Interval] >7 or [Interval]=0 then [Date] else Date.AddDays([Date],-1)
or
= if [Interval] <8 and [Interval] > 0 then Date.AddDays([Date],-1) else [Date]
That will take all hours [8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,0] and use the current date and will take all hours [1,2,3,4,5,6,7] from the next date.
Then right click ... Group By .. on that new custom column and do operation Sum on Column: Volume of Contact, with whatever name you want in New Column Name
sample full code
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Interval", Int64.Type}, {"Volume of contact", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Interval] >7 or [Interval]=0 then [Date] else Date.AddDays([Date],-1), type date),
#"Grouped Rows" = Table.Group(#"Added Custom", {"Custom"}, {{"Volume of Contact", each List.Sum([Volume of contact]), type number}})
in #"Grouped Rows"
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"
I'm starting to use Power Query in Excel 365 (desktop install). Is there a way to change the column name to append or prepend today's date to the column name? If the column is named "Size" I'd like the column to be named "Size_2019_04_18". The exact format of the date doesn't matter.
1, Go to Power Query Editor
2, Go to Advanced Editor
3, add the code below (Case Sentitive):
Let
...
NewName = "Size_"&Date.ToText(DateTime.Date(DateTime.LocalNow())),
#"Changed Type" = Table.TransformColumnTypes(Sheet1_Table,{{"Size", Int64.Type}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Size", NewName}})
in
#"Renamed Columns"
Test Result:
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"
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"