I have a table which shows sales depending on the source (fact and forecast).
year
week
category
sales rub
source
2021
32
shorts
54387
2021 fact
2021
32
shorts
58264
forecast
2021
33
dresses
4325
2021 fact
2021
33
dresses
5432
forecast
When I make a matrix in powerBI need to get a deviation fact from forecast, bu I cannot make a quick measure division because in fact i have only one column with values. How can i calculate the deviation? Thanks a lot
If using powerquery, load data
click select source column
transform .. pivot column ... and for values column, choose sales rub from dropdown
add column .. custom column. Name it deviation with formula
= [2021 fact]-[forecast]
full sample code:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Pivoted Column" = Table.Pivot(Source, List.Distinct(Source[source]), "source", "sales rub", List.Sum),
#"Added Custom" = Table.AddColumn(#"Pivoted Column", "deviation", each [2021 fact]-[forecast])
in #"Added Custom"
Related
I'm working with amazon-quicksight and I'm trying to migrate one funcionality from PowerBi to Quicksight but it has not been possible.
The idea is: I have a dataset with two columns "date" and "sales". The user will have a filter with the column "date". More than one date can be selected. Depending on the dates selected by the user we need to get two KPIs, the first one is the sum of sales for those dates (this is already done), but the second one is my problem, it should be the sum of the sales on the same days selected by the user but in the previous year (it depends on which year was selected for each date).
Example:
DataSet:
DATE
SALES
2020-01-05
1
2020-02-01
1
2020-06-10
4
2020-06-17
1
2021-01-01
1
2021-02-01
3
2021-06-10
3
2021-06-15
5
If the user select the dates: 2021-02-01, 2021-06-10 and 2021-06-15, the result should be:
KPI 1: Sum of sales (for those dates): 11
KPI 2: Sum of sales for those dates in the previous year: 5 -> (Days to use 2020-02-01, 2020-06-10 and 2020-06-15)
Do you have any idea about how can I calculate the KPI 2? Any suggestion?
In powerBI this (KPI 2) was done with the function: SAMEPERIODLASTYEAR
Thanks in advance.
I was able to solve this issue taking into account that the user must filter data from the two years, for example 2020 and 2021.
After that, I created the "calculated field":
calc_sum_sales = sum(sales)
After that:
calc_sum_sales_last_year = ({calc_sum_sales}-periodOverPeriodDifference(sum(sales),date,YEAR,1))
After that, I created an "visual table" and added in "group by" the field "date" and in "value" the field "calc_sum_sales_last_year"
I must give click in the column date to agroup by "year". In that way I get just two rows, one row for each year.
Finally, I change the "visual table" to "KPI" and I got the expected result.
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"
the starting point is the following table in which entries are made for events on specific days (journal).
Entity
Event
Date
Amount
0123
acquisition
05.05.2015
10,000.00
0123
capital increase
30.11.2015
1,000.00
0123
write-off
31.12.2017
-4,000.00
0123
write-up
31.12.2019
3,000.00
This journal is loaded into Power Query to be enhanced with additional information from other sources.
The goal is a Power Pivot table in which the amounts are summarized as at 31.12. of each year (Subtotals).
Year
Entity
Event
Date
Amount
2015
0123
aquisition
05.05.2015
10,000.00
2015
0123
capital increase
30.11.2015
1,000.00
2015 Subtotal
0123
11,000.00
2016 Subtotal
0123
11,000.00
2017
0123
write-off
31.12.2017
-4,000.00
2017 Subtotal
0123
7,000.00
2018 Subtotal
0123
7,000.00
2019
0123
write-up
31.12.2019
3,000.00
2019 Subtotal
0123
10,000.00
2020 Subtotal
0123
10,000,00
The question is how to insert rows in Power Query for years where no activity (event) has occurred (no entry in the journal) so that a subtotal can be shown in Power Pivot as of 31.12. of each year.
I hope I could explain my issue in an understandable way. Thanks in advance for your help!
Kind regards,
Joerg
See if something like this works for you. There are shorter, more confusing ways to do it
Get minimum year of all the data, and maximum year of all the data, and create a table of all combinations of years and entities. See if those are being used. If not, merge that year and entity back into the original table with month=dec day=31
there is a bit of self-merging etc, which requires pasting this into home...advanced... since not all of it can be done in the user interface
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Entity", Int64.Type}, {"Event", type text}, {"Date", type date}, {"Amount", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Year", each Date.Year([Date])),
// Create table of all possible Entities and Years
DateList = {Date.Year(List.Min(#"Added Custom"[Date])) .. Date.Year(List.Max(#"Added Custom"[Date]))},
Entities = Table.AddColumn(Table.Distinct(Table.SelectColumns(#"Added Custom",{"Entity"})),"Year", each DateList),
#"Expanded Year" = Table.ExpandListColumn(Entities, "Year"),
// Find unique Data and merge into original data set
#"Merged Queries" = Table.NestedJoin(#"Expanded Year",{"Year", "Entity"},#"Added Custom",{"Year", "Entity"},"Table2",JoinKind.LeftOuter),
#"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Date"}, {"Date2"}),
#"Filtered Rows" = Table.SelectRows(#"Expanded Table2", each ([Date2] = null)),
#"Added Custom1" = Table.AddColumn(#"Filtered Rows", "Date", each #date([Year],12,31), type date),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Date2", "Year"}),
#"Appended Query" = Table.Combine({#"Changed Type", #"Removed Columns" })
in #"Appended Query"
I have a detail table in my model with a row for every hour in a day and the sales amount generated to that point of the date. Its not displayed on my report, rather it acts as the base for another reference table in the model:
Datetime Sales
2019-11-10 06:00:00 100.00
2019-11-10 12:00:00 200.00
2019-11-10 18:00:00 500.00
2019-11-10 23:59:59 999.00
The first (reference) table in the model only displays in my report the last hour value of each day in my base table. they are related via the datetime field using a 1:M with a single cross filter setting:
let
Source = Base,
#"Filtered Rows" = Table.SelectRows(Source, each ([Datetime] = #time(23, 59, 59)))
in
#"Filtered Rows"
Datetime Sales
2019-11-10 23:59:59 999.00
2019-11-11 23:59:59 950.00
2019-11-12 23:59:59 900.00
I would like to place a third table on my report that shows each of the hourly rows from the base table when a user clicks on a row in the daily table with the default setting being today's data which is added hourly.
I went down the path of using Power Query to try and filter the base table but have not been able to make it work. Should this instead be done using DAX? Either way, what would the query look like?
thanks in advance!
Thanks, but I have just resolved this. By changing my relationship to use a date field instead of the datetime field, and changing it to a (*:1) type, i am now getting the filter to do what I want. Now just have to figure out how to default to today's date. Thanks again!
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"