Can PowerQuery append rows based on columns? - powerbi

Hello StackOverflow Community,
I have a Dataset where the monthly volume's values per Model is shown in multiple columns instead of one. I.E:
Model Jan Feb Mar Apr May
A 5 12 26 56 23
Is it possible through PowerBI's PowerQuery to create one "Month" column where its values are being appended? Example:
Model Month Volume
A Jan 5
A Feb 12
A Mar 26
A Apr 56
A May 23
Thanks in advance.

You need to use 'Unpivot columns', in the transform options of the query editor
Select the columns you want, then Unpivot.
You should give you this:
It will name the new columns Attribute and Value, you can then rename them to what you want. FYI when the next month (Jun) is added to the source it should unpivot that column automatically.

Related

Filter only MAX month figures Power BI Desktop

I have a table of values for orders per month by region that looks like this:
Orders Table
Orders (YTD)
Month
Year
1
Jan
2021
4
Feb
2021
4
Mar
2021
5
Apr
2021
14
May
2021
16
Jun
2021
17
Jul
2021
19
Aug
2021
22
Sep
2021
24
Oct
2021
34
Nov
2021
35
Dec
2021
1
Jan
2022
3
Feb
2022
4
Mar
2022
Along with a table that orders the months in sequence as below, that will be modelled to order the months in the first table so that they appear in sequence in graphs.
Monthly Sequence Table
Month Sequence
Month
1
Jan
2
Feb
3
Mar
4
Apr
5
May
6
Jun
7
Jul
8
Aug
9
Sep
10
Oct
11
Nov
12
Dec
Upon closer inspection of my data, I have realised that the number of orders per month are not the raw figure per month, but a cumulative total for every order in the calendar year so far (new orders for month + orders for preceding month). Firstly, I want to calculate the correct sum of orders per year, which should actually just be the MAX month from the orders table. Of course in most years this will be December, but for the current year it needs to be the latest month. I wanted to use a measure to calculate the MAX 'Monthly Sequence Table'[Month Sequence] number from each table, by year. I thought maybe a filter function would be used but could not work out exactly how to do this in DAX.
Secondly, and similarly, I want to calculate the actual number of orders per each individual month using DAX. In this case, I want to take the Orders (YTD) total for that month/year combination and subtract it from its immediately preceding month. What would the formula look like for this?
Thanks in advance.

Power BI - New rows with inhereted values

got table
Period
Entity
Value
NOV 2020
Entity 1
20
DEC 2020
Entity 1
30
JAN 2021
Entity 1
25
Now I want to insert row for every period with same value but different entity, so my table looks like this:
Period
Entity
Value
NOV 2020
Entity 1
20
NOV 2020
Entity 2
20
DEC 2020
Entity 1
30
DEC 2020
Entity 2
30
JAN 2021
Entity 1
25
JAN 2021
Entity 2
25
Is it something I have to do in query editor through new custom column? Or just through new measure after data load?
Basically what I really need is to present in visual "Entity 2" with same values as "Entity 1" for multiple preiods.
Any ideas?
Thank you
Follow these below steps in Power Query Editor-
Step-1: Duplicate your base table.
Step-2: Replace value "Entity 1" with "Entity 2" in the Entity column of your new table.
Step-3: Now select Append Query as Ne option from the ribbon and select both old and new table.
Finally you will find the below output in your newly created/appended table-

Create buckets to locate months in power BI

I have a column in excel named Month which consists data like-
When you create a chart with E_no by month you will get something like this
This is what the column exactly means
(In Jan 765 people were hired
In jan-feb-Mar 2276 people were hired)
How the chart looks like
Jan. -----------765
Jan-feb-mar ---------------------- 2276
Sep-Oct. ---------- 10
Oct. -------- 2
(the dashes are basically the bars of a horizontal bar graph and Jan, jan-feb-Mar as my Y axis)
E no
Month
2038
feb-mar-jun
657
Sep-Oct
221
Jun
6507
Oct
876
Mar-Apr-May
17
Nov-Dec
615
Dec
Now what I want is to create buckets like June should fall under the category of feb-mar-jun , Dec should fall under the category of Nov-dec, Oct should fall under the category of sep-oct.
for eg -
From these
Jan. -----------765
Jan-feb-mar ---------------------- 2276
Sep-oct. ---------10
Oct ----2
My o/p should look like
Jan-feb-mar -------------------------------- 3041
Sep-Oct ------------- 12
I'm new to power bi so if anyone have any good method other than this what I have mention would really help me to to get rid of this.
Let me know if you have any doubt regarding my question
Thanks in advance

Django ORM group by, and find latest item of each group (window functions)

Say we have a model as below
class Cake(models.Model):
baked_on = models.DateTimeField(auto_now_add=True)
cake_name = models.CharField(max_length=20)
Now, there are multiple Cakes baked on the same day, and I need a query that will return me a monthly cake report which consists of each day of the month, and the names of the first and last cakes baked on that day.
For example, if the data is something like this:
baked_on cake_name
11 Jan 12:30 Vanilla
11 Jan 14:30 Strawberry
11 Jan 20:45 Avocado
12 Jan 09:05 Raspberry
12 Jan 16:30 Sprinkles
12 Jan 20:11 Chocolate
My query's output should look like
date first last
11 Jan Vanilla Avocado
12 Jan Raspberry Chocolate
How should I go about doing this in a single ORM call?
Django 2.0 introduced window functions that are made for that kind of queries. Simple answer for your question will be:
Cake.objects.annotate(
first_cake=Window(
expression=FirstValue('cake_name'),
partition_by=[TruncDate('baked_on')],
order_by=F('baked_on').asc(),
),
last_cake=Window(
expression=FirstValue('cake_name'),
partition_by=[TruncDate('baked_on')],
order_by=F('baked_on').desc(),
),
day=TruncDate('baked_on'),
).distinct().values_list('day', 'first_cake', 'last_cake')
Why FirstValue in last_cake? That's becaues window query by default will traverse through each row and won't look ahead, so for every row, last row will be equal to current row. Using last_row together with descending sorting will fix that. Either that or you can define frame for which window query should work:
Cake.objects.annotate(
first_cake=Window(
expression=FirstValue('cake_name'),
partition_by=[TruncDate('baked_on')],
order_by=F('baked_on').asc(),
),
last_cake=Window(
expression=LastValue('cake_name'),
partition_by=[TruncDate('baked_on')],
order_by=F('baked_on').asc(),
frame=ValueRange(),
),
day=TruncDate('baked_on'),
).distinct().values_list('day', 'first_cake', 'last_cake')

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: