Power Query -- Group-by-Result Expand - powerbi

Here's an example of my existing data set. I have unique parts (Part) grouped by what factory # (Factory) they're used in and when the parts started to be used in operation (Part Install Year).
Part
Factory
Part Install Year
1
100
2018
2
100
2018
3
100
2018
3
200
2019
3
300
2020
4
400
2019
5
400
2020
6
500
2018
Desired Output is below. I need to group all the related parts by the lowest numbered factory they are installed in (Part Grouping) and then calculate the earliest year any part was installed in that factory (Factory Install Year). I'm having trouble figuring out how to create the Factory Install Year. Thank you!
Part
Factory
Part Install Year
Part Grouping
Factory Install Year
1
100
2018
100
min of all Dates in Factory 100
2
100
2018
100
min of all Dates in Factory 100
3
100
2018
100
min of all Dates in Factory 100
3
200
2019
100
min of all Dates in Factory 100
3
300
2020
100
min of all Dates in Factory 100
4
400
2019
400
min of all Dates in Factory 400
5
400
2020
400
min of all Dates in Factory 400
6
500
2018
500
2018

Try
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Part Install Year", type number}, {"Factory", type number}}),
EarliestYear = Table.Group(#"Changed Type", {"Factory"}, {{"EarliestYear", each List.Min([Part Install Year]), type nullable number}}),
#"Grouped Rows" = Table.Group( #"Changed Type", {"Part"}, {
{"data", each _, type table },
{"MinFactory", each List.Min([Factory])},
{"EarliestYear", each try EarliestYear[EarliestYear]{List.PositionOf(EarliestYear[Factory],List.Min([Factory]))} otherwise null}
}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"Factory", "Part Install Year"}, {"Factory", "Part Install Year"})
in #"Expanded data"

Related

Power BI filter over 6 month and last 6 month

I would like to create a button filter in power bi that would allow me to show the values ​​that have been active for more than 6 months and those that have been active for less than 6 months, I will explain an example:
DATE
IMPORT
CLIENT_ID
2022-11-01
55.56
C001
2022-10-01
23.5
C001
2022-09-01
12.6
C002
2022-03-01
56.2
C003
2022-02-01
45.2
C001
2022-01-01
34.5
C003
Then in Power Bi have a filter like this:
So when I click on "last 6 month" in the matrix will appear:
Matrix result applied "last 6 month"
And if I click on "over 6 month" the matrix will change the data for:
Add a calculated column with the following expression
period name =
IF (
'Table'[DATE] < DATE( YEAR(TODAY()), MONTH(TODAY()) - 6, DAY(TODAY())),
"over 6 month",
"last 6 month"
)
to your 'Table' and use it in your slicer.

Merging multiple parameters into the same row in Power Query

I got the following table I want to edit with power query (still a beginner).
Every month we got a new row for every parameter (many) for every object. What I want, is a table with a new row for every every object and every Month with all the parameters listed as columns. The parameters can include numbers, dates, empty values etc.
I hope I could explain my issue well enough.
Thanks for you help!
What I have:
Parameter
Object
Location
Size
Month
Value
1
Object A
USA
4
Jan 2002
180
1
Object A
USA
4
Feb 2002
210
2
Object A
USA
4
Jan 2002
312
2
Object A
USA
4
Feb 2002
140
1
Object B
CAN
6
Jan 2002
164
1
Object B
CAN
6
Feb 2002
130
2
Object B
CAN
6
Jan 2002
95
2
Object B
CAN
6
Feb 2002
122
What I want:
Object
Month
Location
Size
Parameter 1
Parameter 2
Parameter 3...
Object A
Jan 2002
USA
4
180
312
...
Object A
Feb 2002
USA
4
210
140
...
Object B
Jan 2002
CAN
6
164
95
...
Object B
Feb 2002
CAN
6
130
122
95
Load data into powerquery with data .. from table/range... [x] headers
click select parameter column
transform .. pivot column
values column:value [ok]
file ... close and load ...
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Parameter", Int64.Type}, {"Object", type text}, {"Location", type text}, {"Size", Int64.Type}, {"Month", type datetime}, {"Value", Int64.Type}}),
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Changed Type", {{"Parameter", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Changed Type", {{"Parameter", type text}}, "en-US")[Parameter]), "Parameter", "Value", List.Sum)
in #"Pivoted Column"

Power query (powerbi) transforming panel data (cross-sectional-temporal data) into a cross-sectional structure with added temporal columns

I am working in powerbi and I have a problem in the power query editor. My source table contains cross-sectional-temporal data with both cross-sectional and temporal primary keys (2 identifiers):
primary_key time_year data_feature_A
1 2019 A
1 2020 C
1 2021 L
1 2022 B
2 2019 K
2 2020 H
2 2021 M
2 2022 D
3 2019 A
3 2020 F
3 2021 X
3 2022 Y
4 2019 Y
4 2020 D
4 2021 M
4 2022 H
At the end of the procedure, the table should have the following structure:
primary_key 2019 2020 2021 2022
1 A C L B
2 K H M D
3 A F X Y
4 Y D M H
How do I do this most efficiently in Power Query? I would like to run such a procedure on a very big data set (original data).
In powerquery, just pivot the data
click select the time_year column
transform...pivot column...
values column:data_feature_a
advanced options...dont aggregate
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(Source, {{"time_year", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(Source, {{"time_year", type text}}, "en-US")[time_year]), "time_year", "data_feature_A")
in #"Pivoted Column"

Power BI - Calculate Percentage Measure based on Top N Selection

I have a table like this.
Company Amount Year
A 200 2016
B 300 2016
C 400 2016
A 500 2017
B 600 2017
C 700 2017
A 100 2016
B 400 2016
C 100 2016
A 600 2017
B 133 2017
C 50 2017
I am looking for a measure calculate the Percentage of amount that top 2 companies(based on amount) contributes to that particular year's total amount. This needs to be dynamic based on the values of Year slicer. (For Example if 2 years are selected, then the top 2 companies needs to be based on the total amount that the company has spent on those 2 years).
How about this as a measure?
PercentTop2 =
DIVIDE(
SUMX(
TOPN(2,
SUMMARIZECOLUMNS(Companies[Company],
"Amount", SUM(Companies[Amount])),
[Amount]),
[Amount]),
SUMX(ALLSELECTED(Companies), Companies[Amount]))
The TOPN(2,[...]) finds the top 2 rows of the summarized table. Then you divide the sum of those two rows by the sum of all the selected rows.

Calculate Variance in PowerBI using dax query

I am trying to create a variance measure in PowerBI.
This is the data that I have,
Month Year MonthNo Value
Jan 2016 1 700
Feb 2016 2 800
March 2016 3 900
April 2016 4 750
.
.
Jan 2017 13 690
Feb 2017 14 730
And My variance for the Month Number 7 should be like,
`{Avg(values(4,5,6) - Value(7)} / Value(7)`
i.e (Average of last 3 months value - current month value) / Current month value
How to do this in Power BI? Thanks.
If it is okay for you to use a column, I believe you could add one with this code to get what you want:
Variance = (CALCULATE(AVERAGEX(Sheet1,Sheet1[Value]),FILTER(FILTER(Sheet1,Sheet1[MonthNo]<=EARLIER(Sheet1[MonthNo])-1),Sheet1[MonthNo]>=EARLIER(Sheet1[MonthNo])-3))-Sheet1[Value])/Sheet1[Value]
You'll need to replace all instances of Sheet1 with the name of your table.
It'll give you something like this: