Creating a new column according to reference table - powerbi

​
I am relatively new to PowerBI and I was wondering if someone would be able to help with something I am stuck on. I have the following table (ProductDate) with information in the format below:
Product number
Date
Day of week
5JR38
16 September 2022
Friday
5JR38
17 September 2022
Saturday
5JR38
18 September 2022
Sunday
7QP13
12 September 2022
Monday
7QP13
13 September 2022
Tuesday
7QP13
14 September 2022
Wednesday
7QP13
15 September 2022
Thursday
7QP13
16 September 2022
Friday
7QP13
17 September 2022
Saturday
7QP13
18 September 2022
Sunday
​
I also have another table (ProductStock) with the number of stock available for a specific day of the week range:
Product number
Day ref
Stock
5JR38
FriO
20
5JR38
WEnd
65
7QP13
MFriO
7
7QP13
MidWeek
13
7QP13
WEnd
18
​
The Day ref column in the table above refers to a range of dates which are stored in a separate table (DayReference) as follows:
Day ref
Day of the week
FriO
Friday
MFriO
Monday
MFriO
Friday
WEnd
Saturday
WEnd
Sunday
MidWeek
Tuesday
MidWeek
Wednesday
MidWeek
Thursday
​
The 'Day ref' column in the ProductStock table does not overlap (i.e. for a 'Product Number' there is no 'Day ref' which would refer to a day of the week twice).
What I am trying to do is create a new column using DAX in the first table (ProductDate) with the 'Day ref' populated from the ProductStock tablewith their corresponding day of the week. The expected result should be something like this:
Product number
Date
Day of week
Relevant Day ref
5JR38
16 September 2022
Friday
FriO
5JR38
17 September 2022
Saturday
WEnd
5JR38
18 September 2022
Sunday
WEnd
7QP13
12 September 2022
Monday
MFriO
7QP13
13 September 2022
Tuesday
MidWeek
7QP13
14 September 2022
Wednesday
MidWeek
7QP13
15 September 2022
Thursday
MidWeek
7QP13
16 September 2022
Friday
MFriO
7QP13
17 September 2022
Saturday
WEnd
7QP13
18 September 2022
Sunday
WEnd
I don't really want to create a many-to-many relationship between those tables and would prefer if I add a new column as shown above. Any help or guidance will be much appreciated.
​
Thank you

You can achieve this in the Advanced Query Editor. Just open the Advance Editor for table ProductDate and try to incorporate this below code there-
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMvUKMrZQ0lEyNFMITi0oSc1NSi1SMDIwMgKKuRVlpiRWKsXqICkzx1QWnFhSWoSh0AKLwtI8mDLzwABDY5AyI0xlvvmYyowxlYWUphZjqDPBVBeempKHRaUpFhMzSoswFeIPGLgyQgEDV4gnYGIB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Product number" = _t, Date = _t, #"Day of week" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Product number", type text}, {"Date", type date}, {"Day of week", type text}}),
//--------------------------
//New steps started from here
//---------------------------
#"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Day of week"}, DayReference, {"Day of the week"}, "DayReference", JoinKind.LeftOuter),
#"Expanded DayReference" = Table.ExpandTableColumn(#"Merged Queries", "DayReference", {"Day ref"}, {"DayReference.Day ref"}),
#"Merged Queries1" = Table.NestedJoin(#"Expanded DayReference", {"Product number", "DayReference.Day ref"}, ProductStock, {"Product number", "Day ref"}, "ProductStock", JoinKind.Inner),
#"Expanded ProductStock" = Table.ExpandTableColumn(#"Merged Queries1", "ProductStock", {"Day ref"}, {"ProductStock.Day ref"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded ProductStock",{"DayReference.Day ref"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"ProductStock.Day ref", "Relevant Day ref"}}),
#"Sorted Rows" = Table.Sort(#"Renamed Columns",{{"Product number", Order.Ascending}, {"Date", Order.Ascending}})
in
#"Sorted Rows"
Here is the output-

Related

Power Query -- Group-by-Result Expand

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"

Apply PERCENTILE.INC on aggregated results in Power BI

I have the following dataset 'Table1'
City
Brand
Year
Number_of_Customers
London
A
2019
387
London
A
2020
1566
London
A
2021
1409
Manchester
A
2019
353
Manchester
A
2020
679
Manchester
A
2021
1099
Bristol
A
2019
2999
Bristol
A
2020
2654
Bristol
A
2021
426
York
A
2019
214
York
A
2020
948
York
A
2021
1948
Birmingham
A
2019
452
Birmingham
A
2020
2465
Birmingham
A
2021
1856
London
B
2019
1829
London
B
2020
1236
London
B
2021
2960
Manchester
B
2019
2593
Manchester
B
2020
533
Manchester
B
2021
126
Bristol
B
2019
1588
Bristol
B
2020
2067
Bristol
B
2021
1823
York
B
2019
1667
York
B
2020
2931
York
B
2021
657
Birmingham
B
2019
2896
Birmingham
B
2020
421
Birmingham
B
2021
2488
I wish to apply PERCENTILE.INC on the following 'Table2' where [Number_Of_Customers] is aggregated on [Brand] and [Year] level
City
Number_Of_Customers
Birmingham
...
Bristol
...
London
...
Manchester
....
York
....
where in 'Table2' I filter columns [Brand] and [Year] with multi-valued filters,
e.g. for [Brand] = "A" and [Year] = {"2019","2020"} the 75th percentile should be 2917 (I get this result from EXCEL function PERCENTILE.INC).
So far used the following formula
75th_Percentile =
CALCULATE(
PERCENTILE.INC(Table1[Number_of_Customers],0.75),
ALL('Table1'[City])
)
but this doesn't work ( see image below where I get wrong value [75th_Percentile] = 2240,25 ).
Any ideas how to solve this?
Second try:
75th_Percentile =
VAR a = CALCULATETABLE(ADDCOLUMNS( SUMMARIZE( Table1 , Table1[City]), "#total", CALCULATE(SUM(Table1[Number_of_Customers]))), ALLSELECTED(Table1))
RETURN PERCENTILEX.INC (a, [#total],0.75)
First try:
What happens if you remove the calculate?
75th_Percentile =
PERCENTILE.INC(Table1[Number_of_Customers],0.75)

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"

How can I create a dynamic measure or column for an array in PowerBI?

Excel Example
I am attempting to recreate a similar chart in PowerBI as I did in excel seen below:
Here I have revenue per day. The chart shows the percent of days where revenue exceeds a fixed amount (100, 200, etc).
In PowerBI I know how to recreate the table that the chart is based on by defining a column, but it's not dynamic. I can't apply filters to change the column values.
I know I can apply filters to measures but when I try to replicate the formula as a measure I get an error, which I assume is due to the formula trying to return an array of values.
Here is my formula for the fixed column version:
table2 column = countx(
filter(
DayRevenueTable,
[Revenue]>Table2[DayRevenueExceeding])
,[Day])
/Total
Assuming your table looks like this:
Date
Revenue
04 January 2022
102
11 January 2022
162
17 January 2022
180
02 January 2022
185
12 January 2022
203
05 January 2022
278
01 January 2022
353
16 January 2022
449
14 January 2022
500
06 January 2022
515
08 January 2022
582
10 January 2022
600
03 January 2022
618
09 January 2022
626
13 January 2022
626
15 January 2022
706
18 January 2022
765
07 January 2022
895
You need to first create a table with your fixed values. Basically is the same concept as creating a parameter.
Using that table as a reference, you can create your calculation around Fixed Values[Value].
DAX: Fixed Values
Fixed Values = GENERATESERIES(100,1000,100)
DAX: Days When Revenue Exceeds Amount
Days When Revenue Exceeds Amount =
VAR CurrentFixedValue =
SELECTEDVALUE ( 'Fixed Values'[Value] )
VAR CountValues =
CALCULATE (
COUNTROWS ( 'Table' ),
'Table'[Revenue] < ( CurrentFixedValue + 100 )
)
VAR AllValues =
CALCULATE ( COUNTROWS ( 'Table' ), ALLSELECTED ( 'Table' ) )
VAR Calc =
DIVIDE ( CountValues, AllValues )
RETURN
Calc
Output

Django filter query week day wise(Friday to Thursday consecutively in a month)

HI Guys I have a datas in the model like this
Date Day Amount
------------------------------
01/09/2016 Thursday 2500
02/09/2016 Friday 300
03/09/2016 Saturday 600
04/09/2016 Sunday 7500
05/09/2016 Monday 9800
06/09/2016 Tuesday 2800
07/09/2016 Wednesday 3600
08/09/2016 Thursday 580
09/09/2016 Friday 352
10/09/2016 Saturday 950
11/09/2016 Sunday 780
12/09/2016 Monday 650
13/09/2016 Tuesday 440
14/09/2016 Wednesday 25
15/09/2016 Thursday 39
16/09/2016 Friday 500
17/09/2016 Saturday 51
18/09/2016 Sunday 65
19/09/2016 Monday 99
20/09/2016 Tuesday 350
21/09/2016 Wednesday 280
22/09/2016 Thursday 782
23/09/2016 Friday 98
24/09/2016 Saturday 785
25/09/2016 Sunday 965
26/09/2016 Monday 1500
27/09/2016 Tuesday 3650
28/09/2016 Wednesday 85
29/09/2016 Thursday 70
30/09/2016 Friday 980
I want to write a filter query in django which filters the data like Sum of Friday to Thursday consecutively following next, of every month.
i.e sum(02/09/2016(Friday) To 08/09/2016(Thursday)) , sum(09/09/2016(Friday) To 15/09/2016(Thursday)) and so on....
You can use django's raw query method to fire below query for your mentioned data
[Model_Name.objects.raw('query_below_mentioned')][1]
. I'm not aware about the DB you are using but you can use below mentioned query for fetching data . I tried it is sqlite and it's working fine, but the only problem here is if your data keeps increasing then you have to manually adjust offset and modify the query. :( ,but if it's urgent you can use it as of now:
select * from (select sum(amount) as sum from (SELECT * FROM demo_table LIMIT 7 OFFSET 0)
UNION
select sum(amount) as sum from (SELECT * FROM demo_table LIMIT 7 OFFSET 7)
UNION
select sum(amount) as sum from (SELECT * FROM demo_table LIMIT 7 OFFSET 14)
UNION
select sum(amount) as sum from (SELECT * FROM demo_table LIMIT 7 OFFSET 21));