How can I transpose multiple columns at once? - sas

I am trying to transpose three columns by two variables.
My current dataset looks like:
Person Date Company Industry Number
John 2017 Apple Tech 5
John 2017 Starbucks Beverages 3
Kim 2014 Hilton Hotels 9
I would like my output data set to look like:
Person | Date | Company1 | Industry1 | Number1 | Company2 |Industry2| Number2
John | 2017 | Apple | Tech | 5 | Starbucks| Beverage| 3
Kim | 2014 | Hilton | Hotels | 9 | - | - | -
As you can see, I would like each observation to be unique by name and date.
Any suggestions?

Related

PowerBI Ranking based on multiple columns

i am writing a dax formula to rank my products based on multiple column
input from 3 different tables
channel table - channel column
fruits table - brand, fruits, serving size columns
sales table - total sales column (calculated)
channel | brand | fruits | serving size | Total Sales
online | sunny | banana | slice| 100
shop | sunny | apple |whole| 90
amazon | bert | banana | whole | 89
shop | crystal | strawberry | slice|56
output table
channel | brand | fruits | serving size | Total Sales | Fruit_Rank
online | sunny | banana | slice| 100 | 1
shop | sunny | apple |whole| 90 | 2
amazon | bert | banana | whole | 89 | 3
shop | crystal | strawberry | slice|56 | 4
my current dax is
Fruit_Rank = RANKX(CROSSJOIN(ALL('Dim Channel'[channel]),ALL('Dim Fruits'[brand]),ALL('Dim Fruits'[fruits]),ALL('Dim Fruits'[serving size])),[Total Sales])
it gave me the wrong ranking.
Any help is appreciated!
thank you!

How do I repeat row labels in a matrix?

I have data showing me the dates grouped like this:
For security reasons, I had to remove the Customer Description detail, due to confidentiality.
How do I repeat the date column the same way you repeat the Row Labels in an Excel Pivot?
I've looked, but couldn't find a solution to this - this option should be available.
EDIT
When you have the following source data in Excel:
Date | Customer | Item Description | Qty Out | Unit Price | Sales
--------------------------------------------------------------------------------------------------------------------------------------------
14/08/2020 | Customer 1 | Item 11 | 4.00 | 65.00 | 260.00
14/08/2020 | Customer 2 | Item 12 | 56.00 | 12.00 | 672.00
14/08/2020 | Customer 3 | Item 13 | 64.00 | 35.00 | 2,240.00
14/08/2020 | Customer 4 | Item 14 | 29.00 | 65.00 | 1,885.00
15/08/2020 | Customer 2 | Item 15 | 746.00 | 12.00 | 8,952.00
15/08/2020 | Customer 3 | Item 16 | 14.00 | 75.00 | 1,050.00
15/08/2020 | Customer 4 | Item 17 | 45.00 | 741.00 | 33,345.00
15/08/2020 | Customer 5 | Item 18 | 456.00 | 125.00 | 57,000.00
15/08/2020 | Customer 6 | Item 19 | 925.00 | 17.00 | 15,725.00
16/08/2020 | Customer 4 | Item 20 | 6.00 | 532.00 | 3,192.00
16/08/2020 | Customer 5 | Item 21 | 56.00 | 94.00 | 5,264.00
16/08/2020 | Customer 6 | Item 22 | 546.00 | 37.00 | 20,202.00
You then pivot this data using Microsoft Excel, where you get the following:
You then choose the option to Repeat Item Labels as can be seen below:
After selecting this, you get my expected results I require in Power BI:
Is there not a function available like this in Power BI?
Just adding this for your reference as a work around. Check this below image with a custom column created in the Power Query Editor-
date_customer = Date.ToText([Date]) &" : "& [Customer]
Then added both Date and date_customer in the Matrix row level. The output is as below- (using your sample data)
ANOTHER OPTION Another option is to add Date and Customer in the Matrix row and the output is will be as below- (using your sample data)
This is also a meaningful output as date are showing as a group header. But in case of requirement of having redundant date to show, you can consider the first option.

How to duplicate values from grouped measures into additional virtual groups

I have a table with places and corresponding population per year. Currently I only have data up to year 2018. Somethin like this:
+-------+------+------------+
| Place | Year | Population |
+-------+------+------------+
| a | 2017 | 12 |
| a | 2018 | 11 |
| b | 2017 | 43 |
| b | 2018 | 21 |
+-------+------+------------+
I've created a measure that sums the "Population" column values and used it in a columns chart with the "Year" column as the axis to see the total population per year. The data in the chart would be:
+------+-------+
| Year | Total |
+------+-------+
| 2017 | 55 |
| 2018 | 33 |
+------+-------+
I need the chart to also show years 2019 and 2020 with the same value from 2020, like:
+------+-------+
| Year | Total |
+------+-------+
| 2017 | 55 |
| 2018 | 33 |
| 2019 | 33 |
| 2020 | 33 |
+------+-------+
How can I achieve the above via DAX. I basically need to take the "Total" from the MAX "Year" and dynamically create two additional entries for 2019 and 2020 with the same total from that MAX "Year".
My use case is more involved as then I will have to add another measure to the same columns chart to show how many of that total population attended a particular class. My table with the "class attendance" data does have values up to 2020 which is why I need the total population to go all the way to 2020 and assume that the population for 2019 and 2020 is the same than in 2018 so then I can compare that with class attendance numbers.
Any help or guidance is welcomed.
Thanks.
This can be achieved very easily by power query: (may be there will be a way to do it in measures as well)
Assuming you data would be something like this:
You can go to Transform tab and perform group by like below
Now Select the Fill(Down) in the Transform tab like below
Finally you can in the visualization tab you can create below:

Summarizing row based data over 2 separate columns within the same table

I've got the following table in Power BI:
Date | PersonID | Hours | Age
------------------------------|------
02-jan-18 | 4 | 8 | 3
06-jan-18 | 4 | 6 | 3
01-feb-18 | 4 | 6 | 3
05-feb-18 | 4 | 4 | 4
01-jan-18 | 5 | 6 | 3
01-feb-18 | 5 | 6 | 3
I have rows of data up until a few years back for multiple PersonID's. Most people have multiple rows per month because the data is split out on separate days. For every date, I have that person's age at the time (in this case, PersonID "4" had a birthday between feb 1st and feb 5th).
What I want to do is calculate the amount of hours PER MONTH, PER AGE. My end result should look something like this (average hours per month shown per age):
Age | Average hours per month
----------------------------------
1 | 35
2 | 31
3 | 28
4 | 28
I have no idea how to get started. How can I calculate a sum over 2 columns?
First, create a column on your table that will allow you to group by month:
MonthYear = EOMONTH(HoursAge[Date], 0)
Now you can write a measure that takes an average over a summarized table:
AvgHoursPerMonth = AVERAGEX(
SUMMARIZE(HoursAge,
HoursAge[MonthYear],
HoursAge[Age],
"MonthHours", SUM(HoursAge[Hours])),
[MonthHours])
Here's what the summarized table looks like for your given example:
This would give the following result when you put the measure into a table with age on the rows:
Age | AvgHoursPerMonth
----|-----------------
3 | 16
4 | 4

PowerBI Sort Columns in Matrix Visual

I have a Matrix visual in Microsoft PowerBI with Australian 'States' as rows and 'Months Ago' as columns.
By default the Matrix shows my columns from 0 months ago to 12. I would like it to show from 12 months ago on the left to 0 months ago on the right.
+-------------------+-----------------------------+-------+
| | Months Ago | |
+-------------------+-----------------------------+-------+
| State | 0 | 1 | 2 | 3 | 4 | 5 | Total |
+-------------------+----+----+----+----+----+----+-------+
| Queensland | 10 | 10 | 10 | 10 | 10 | 10 | 60 |
+-------------------+----+----+----+----+----+----+-------+
| New South Wales | | | | | | | |
+-------------------+----+----+----+----+----+----+-------+
| Victoria | | | | | | | |
+-------------------+----+----+----+----+----+----+-------+
| South Australia | | | | | | | |
+-------------------+----+----+----+----+----+----+-------+
| Western Australia | | | | | | | |
+-------------------+----+----+----+----+----+----+-------+
Currently I am only given the option to sort by the value type fields (ie revenue etc).
Is there any option to sort/order the Column Headers?
I don't think there is an option for you to sort column headers directly.
However, you can change the default sort order for the Months Ago column so that it will be reflected in general.
You can add a custom column MonthSrt = 12 - [Months Ago] in query editor:
(It won't work in DAX because of a known issue)
Then you can select the Months Ago column and sort it by MonthSrt:
The custom sort will be applied when you use the Months Ago column in visuals:
You can also make groups (1 to 1 items) al give them al logical number:
The order will change automaticly in the matrix
The following solution worked for me to display the dates in descending order in a matrix:
how to sort column dates in descending order of matrix in power bi