Hello Everyone I hope you guys are having a great day!Hello lovely people of SO
I have the following dataset:
PRODUCT
REGION
REP
REG-YH-67
NORTH
JANE
REG-YH-68
NORTH
JANE
REG-YH-77
NORTH
JANE
REG-YH-9
NORTH
JANE
REG-YH-30
NORTH
JANE
REG-YH-9
NORTH
JANE
REG-YH-14
WEST
ALEX
REG-YH-33
WEST
ALEX
REG-YH-16
WEST
WILL
REG-YH-16
WEST
WILL
REG-YH-53
WEST
WILL
REG-YH-13
OUT-OF-REG
WILL
REG-YH-42
OUT-OF-REG
MARTHA
REG-YH-67
OUT-OF-REG
MARTHA
REG-YH-68
NORTH
JANE
REG-YH-77
NORTH
JANE
REG-YH-9
NORTH
JANE
REG-YH-30
NORTH
JANE
REG-YH-9
NORTH
JANE
REG-YH-68
NORTH
JANE
REG-YH-77
NORTH
JANE
REG-YH-9
NORTH
JANE
REG-YH-30
NORTH
JANE
REG-YH-9
NORTH
JANE
REG-YH-13
OUT-OF-REG
JANE
REG-YH-42
OUT-OF-REG
JANE
I want to be able to GROUP by REGION and COUNT how many times a product was sold, but I want to be able to create a table that will show me the best top3 selling products by region, I know that first I have to group by REGION and COUNT but I want my final output to look like this:
REGION
1
2
3
NORTH
REG-YH-9
REG-YH-77
REG-YH-30
OUT-OF-REG
REG-YH-13
REG-YH-42
REG-YH-67
WEST
REG-YH-16
REG-YH-14
REG-YH-53
because these are the TOP 3 products by region and I want to be able to see the best selling product (COLUMN ONE) and the the REGION in the left-handside of the table, and also I want this table to be able to recalculate itself if I have a filter in the page that will filter the REP (sales representative) so that my table will update filtering REP--- I have tried day and night to do this guys but power bi isnt like python or R and its really coming for my mental health, so I WILL BE SO BEYOND THANKFUL if you guys can help me out please! I will be very attentive for responses all are welcome! but I am looking for DAX or code answers because I wanna learn, thank you thank you thank you so much
You have a number of tied rankings in your data, but seem to return them at random for your results.
The Power Query code below does the same.
To enter this code, from the Home tab select Transform data, then from the Power Query home tab, select Advanced Editor and paste the code below into the window that opens.
You will need to change the first statement to reflect your actual data source.
let
//Change next line to reflect actual data source
Source = Excel.CurrentWorkbook(){[Name="Table21"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"PRODUCT", type text}, {"REGION", type text}, {"REP", type text}}),
//Group by REGION
//For each Region, Group again by Product and aggregate by count
// Then sort each table by the count
#"Grouped Rows" = Table.Group(#"Changed Type", {"REGION"}, {
{"PRODUCT", each Table.Sort(Table.Group(_,"PRODUCT",{
{"COUNT", each Table.RowCount(_), Int64.Type}
}), {"COUNT", Order.Descending})}
}),
//extract the top 3 into separate columns
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "1", each [PRODUCT][PRODUCT]{0}, type text),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "2", each [PRODUCT][PRODUCT]{1}, type text),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "3", each [PRODUCT][PRODUCT]{1}, type text),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom2",{"PRODUCT"})
in
#"Removed Columns"
Results from your data
Related
GOAL: I want to forecast if Azure Reserved Instances are the right choice for us.
HOW TO DO IT:
I have downloaded the whole Azure Price REST API through this Python script.
I have imported that CSV in an Azure SQL Database
I want to compare each Reserved Instance with the resources we have on Azure thanks to the the Azure Cost Management connector in Power BI Desktop
THE PROBLEM: In a perfect world I would like to see every resources listed like this:
unitPrice
1 Year Reservation
3 Years Reservation
1.2671
6528.3905
12524.2148
But we don't live in a perfect world and the data are organized this way:
unitPrice
meterId
PK
productName
skuName
location
serviceName
unitOfMeasure
type
armSkuName
reservationTerm
6528.3905
003e1713-c374-4003-9a73-27b3ccc80c38
Virtual Machines Ev3 Series - E16 v3 - EU West
Virtual Machines Ev3 Series
E16 v3
EU West
Virtual Machines
1 Hour
Reservation
Standard_E16_v3
1 Year
1.2671
003e1713-c374-4003-9a73-27b3ccc80c38
Virtual Machines Ev3 Series - E16 v3 - EU West
Virtual Machines Ev3 Series
E16 v3
EU West
Virtual Machines
1 Hour
Consumption
Standard_E16_v3
NULL
12524.2148
003e1713-c374-4003-9a73-27b3ccc80c38
Virtual Machines Ev3 Series - E16 v3 - EU West
Virtual Machines Ev3 Series
E16 v3
EU West
Virtual Machines
1 Hour
Reservation
Standard_E16_v3
3 Years
So I created a Primary Key based on the productName, skuName and Location.
I was at the phone with Microsoft and they confirmed that meterId is not a unique identifier.
THE QUESTION: Now that I have a unique identifier I can pivot the 1 Year and 3 Year to put everything on the same row.
tierMinimumUnits
PK
armRegionName
location
meterId
meterName
productId
availabilityId
productName
skuName
serviceName
serviceId
serviceFamily
unitOfMeasure
isPrimaryMeterRegion
armSkuName
effectiveEndDate
RI_unitPrice
RI_DevTestConsumption
RI_1Year
RI_3Years
0.0
Virtual Machines Ev3 Series - E16 v3 - EU West
westeurope
EU West
003e1713-c374-4003-9a73-27b3ccc80c38
E16 v3/E16s v3
DZH318Z0BQ4L
NULL
Virtual Machines Ev3 Series
E16 v3
Virtual Machines
DZH313Z7MMC8
Compute
1 Hour
True
Standard_E16_v3
NULL
1.2671
NULL
0.744739961213781
0.476242102060993
But I ask myself if I'm not doing this wrong.
If the data are on 3 separate rows maybe there is a way through Power Query to keep the data on 3 separate row and write a rule that says
"pick up 1 Year and 3 Years from 3 rows having a uniue identifier"
What is the best approach?
Dataset available on request.
EDIT:
Here are the raw data, I want to target the Virtual Machine D4 v3:
Azure Price List - D4 v3.xlsx : here are the 3 columns productName, skuName, location
Azure Cost Management - D4 v3.xlsx : the 3 previous column need to converge here into ProductName
The column meterId is misleading: it is not a primary key. I also called Microsoft and they confirmed it is not a primary key.
As result I would like to have on the same line: ProductName, effectivePrice, 1Year_unitPrice (need to be pivoted?), 3Years_unitPrice (need to be pivoted?).
I know how to pivot this is SQL. I'm just asking myself if I'm not doing it wrong. Maybe there is a better way to do this in Power BI and I will have less work on the ETL process.
Thank you
It is a bit difficult to understand what your goal is but I think you want the following?
If so, just import your tables into PBI and leave them with no relationship.
Create the following 3 measures.
Effective Price =
VAR productName = SELECTEDVALUE('Azure Price List'[productName])
VAR skuName = SELECTEDVALUE('Azure Price List'[skuName])
VAR location = SELECTEDVALUE('Azure Cost Management'[location])
VAR tempKey = productName + " - " + skuName + " - " + location
VAR result = CALCULATE(MIN('Azure Cost Management'[effectivePrice]), TREATAS({tempKey}, 'Azure Price List'[productName]))
RETURN result
1 Year Price =
CALCULATE(MIN('Azure Price List'[unitPrice]), 'Azure Price List'[reservationTerm] = "1 Year")
3 Year Price =
CALCULATE(MIN('Azure Price List'[unitPrice]), 'Azure Price List'[reservationTerm] = "3 Years")
Add everything to a table:
This seems to work for me.
For this solution, I started from a table in excel. You can change your source appropriate to you. I set up my table in excel to look like your table example, using your data, but then I also added some additional dummy rows.
You may notice I moved the order of the occurrences of the reservationTerms around...not repeating the same pattern for them. I did this because I am not sure if yours will have a pattern, and I use the reservationTerms to name columns later. By moving them around, I made sure my solution would not depend upon them being in any certain order--it should accommodate any order of appearance.
I brought the excel table into Power Query as Table1. I then grouped by productName, skuName, and location, selecting all rows. After that, I did some transformation work within the tables embedded in each group's row. Then I added a column to extract a record with the unit prices from each embedded table in each row. Then I expanded the records. Lastly, I removed the column with the tables that I had created by grouping. The result looks like this.
Here's the M code.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"unitPrice", type number}, {"meterId", type text}, {"PK", type text}, {"productName", type text}, {"skuName", type text}, {"location", type text}, {"serviceName", type text}, {"unitOfMeasure", type text}, {"type", type text}, {"armSkuName", type text}, {"reservationTerm", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"productName", "skuName", "location"}, {{"AllData", each _, type table [unitPrice=nullable number, meterId=nullable text, PK=nullable text, productName=nullable text, skuName=nullable text, location=nullable text, serviceName=nullable text, unitOfMeasure=nullable text, type=nullable text, armSkuName=nullable text, reservationTerm=nullable text]}}),
#"Demoted Headers" = Table.TransformColumns(#"Grouped Rows", {"AllData", each Table.DemoteHeaders(_)}),
Custom1 = Table.TransformColumns(#"Demoted Headers", {"AllData", each Table.Transpose(_)}),
Custom4 = Table.TransformColumns(Custom1, {"AllData", each Table.RenameColumns(_,List.Zip({Table.ColumnNames(_),
Record.ToList(Table.SelectRows(_, each [Column1] = "reservationTerm"){0})}))}),
#"Added Custom" = Table.AddColumn(Custom4, "Custom", each [AllData]{[reservationTerm="unitPrice"]}),
#"Expanded Custom" = Table.ExpandRecordColumn(#"Added Custom", "Custom", {"1 Year", "NULL", "3 Years"}, {"1 Year", "NULL", "3 Years"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"AllData"})
in
#"Removed Columns"
I have a dataset loaded that produces a table that looks like Table 1.
What I'm trying to do is create another table that projects a certain number of months into the future from the most recent month in the dataset for each city, so I can essentially forecast the month-on-month decrease in the number of employees as shown in Table 2.
City
Month Starting
Employees
3 Month Average Attrition
Melbourne
01/12/2012
105
Melbourne
01/01/2022
100
Melbourne
01/02/2022
98
Melbourne
01/03/2022
98
2%
Sydney
01/12/2021
73
Sydney
01/01/2022
70
Sydney
01/02/2022
65
Sydney
01/03/2022
60
7%
City
Month Starting
Employees
Melbourne
01/04/2022
96
Melbourne
01/05/2022
94
Melbourne
01/06/2022
92
Melbourne
01/07/2022
90
Sydney
01/04/2021
56
Sydney
01/05/2022
52
Sydney
01/06/2022
48
Sydney
01/07/2022
45
Thanks in advance.
Try this in powerquery
It first groups and sorts on date, to find latest date. I included the code to filter instead for data in the growth column if that is better. Then generate a list of 0..4 for the next 4 future months. You can set that in the code. Expand. Then use that to calculate the future date using Date.AddMonths and math on the employee count
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"City", type text}, {"Month Starting", type date}, {"Employees", Int64.Type}, {"3 Month Average Attrition", type number}}),
// this next row (filter) can replace the following two rows (group/expand) if desired
// #"Expanded data" = Table.SelectRows(#"Changed Type", each ([3 Month Average Attrition] <> null)),
#"Grouped Rows" = Table.Group(#"Filtered Rows",{"City"}, {{"data", each Table.FirstN(Table.Sort(_,{{"Month Starting", Order.Descending}}),1), type table}}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"Month Starting", "Employees", "3 Month Average Attrition"}, {"Month Starting", "Employees", "3 Month Average Attrition"}),
#"Added Custom" = Table.AddColumn(#"Expanded data", "rows", each List.Numbers(1,4)),
#"Expanded rows" = Table.ExpandListColumn(#"Added Custom", "rows"),
#"Added Custom1" = Table.AddColumn(#"Expanded rows", "Month Starting 2", each Date.AddMonths([Month Starting],[rows]), type date),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "Employees2", each Number.Round([Employees]*Number.Power((1-[3 Month Average Attrition]),[rows]))),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom2",{"Month Starting", "Employees", "3 Month Average Attrition", "rows"})
in #"Removed Columns"
I am trying to concatenate information from two columns into one column in PowerBI. If all the cells were the same, this would be really straight forward. The issue is that I am working with address information that is extracted from a records database with an interesting set-up that I have no control over.
The PowerBI report I have built is used to compare the records database to an online spreadsheet that technicians are using to mark changes that need to be made to the records based on changes they make in a map database. The comparison is done in a PowerBI merged table between records database and the spreadsheet.
Most records are for only one address, while about 10% of the records have multiple address. Currently, the comparison report is telling us that the address do not match on these 10%, even if the addresses are a match.
Data Example of current result only using concatenate:
Row Number
Street Number
Street Name
Concatenated Address
1
234
Harvey St
234 Harvey St
2
246
Malone Ave
246 Malone Ave
3
872, 954
Bluebell Way, Main St
872, 954 Bluebell Way, Main St
4
376, 3457, 78
Harvey St, Bluebell Way, Malone Ave
376, 3457, 78 Harvey St, Bluebell Way, Malone Ave
This is what I am trying to achieve using Dax. So before someone say to split it in the Power Query and create more columns, I'd rather not since the number of address can vary, and I'm already at 46 columns including the ones below.
Data Example of the desired result:
Row Number
Street Number
Street Name
Concatenated Address
1
234
Harvey St
234 Harvey St
2
246
Malone Ave
246 Malone Ave
3
872, 954
Bluebell Way, Main St
872 Bluebell Way, 954 Main St
4
376, 3457, 78
Harvey St, Bluebell Way, Malone Ave
376 Harvey St, 3457 Bluebell Way, 78 Malone Ave
My thought is that maybe there is some way to use a delimiter with concatenating but I am not sure how.
Thank you in advance for anyone who can help me with solving this.
This worked for me in a similar need, although there may be simpler approaches I'm not aware of. Basic steps were to convert the columns to Lists, Zip the Lists, Expand the List, Extract Values, then Group and combine the results. Here's a bit more detail on each step I took:
Add Custom Column named [Custom] with this formula >
Text.Split([Street Number], ",")
Add another Custom Column named [Custom.1] with this formula >
Text.Split([Street Name], ",")
Add another Custom Column named [Custom.2] with this formula >
List.Zip({[Custom], [Custom.1]})
Expanded [Custom.2], formula bar shows this:
= Table.ExpandListColumn(#"Added Custom2", "Custom.2")
Extracted [Custom.2], formula bar shows this:
= Table.TransformColumns(#"Expanded Custom.2", {"Custom.2", each Text.Combine(List.Transform(_, Text.From), " "), type text})
Group on {"Row Number", "Street Number", "Street Name"}, but change the formula bar to use this function instead:
= Table.Group(#"Removed Other Columns", {"Row Number", "Street Number", "Street Name"}, {"Concatenated Address", each Text.Combine([Custom.2], ","), type text})
Here is the full Advanced Editor:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XY6xCsMwDER/5fCspbYTu2M7denUIYPJ4IKGgkmhtIH8fSQMjZNFoNO706VkToaMdV7mLX9mXvD4VgXbPlIyVlXfy7zn8p4Yl5mrhEZQ0okcgyWcO429lh8/uRQMeSFhX1N9IQj2N+H/dw1Stws9wfkuEEJsSxKOwU0rcaEh1X/AQ9zVHlc=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Row Number" = _t, #"Street Number" = _t, #"Street Name" = _t, #"Concatenated Address" = _t]),
#"Added Custom" = Table.AddColumn(Source, "Custom", each Text.Split([Street Number], ",")),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each Text.Split([Street Name], ",")),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "Custom.2", each List.Zip({[Custom], [Custom.1]})),
#"Expanded Custom.2" = Table.ExpandListColumn(#"Added Custom2", "Custom.2"),
#"Extracted Values" = Table.TransformColumns(#"Expanded Custom.2", {"Custom.2", each Text.Combine(List.Transform(_, Text.From), " "), type text}),
#"Grouped Rows" = Table.Group(#"Extracted Values", {"Row Number", "Street Number", "Street Name"}, {"Concatenated Address", each Text.Combine([Custom.2], ","), type text})
in
#"Grouped Rows"
Split Columns into Rows is your key... it is a long shoot to explain, therefore check the sample file and let me know if it works for you...
Sample File
I am working on dynamically changing titles in PowerBI. I have Treemap which displays the population of 6 US states - 2 states from the east (NY, MA) and 3 from the west (CA, OR, WA).
The treemap looks like this -
A user can choose multiple states from a treemap by pressing ctrl and clicking on the square that represents the state.
When the user selects both the eastern states, I want the title to say "population in the east states" and similarly for the west too. Can this be done using DAX?
My title requirements -
If selected states are MA && NY then display "Population in the East States" as the title.
If selected states are CA && OR && WA then display "Population in the West States" as the title.
If only 1 state is selected then display the name of the state in the title.
I could do 3. Can someone please help me with 1 and 2?
Here's a measure that will do this.
Title =
SWITCH( TRUE(),
CONCATENATEX(VALUES(Table3[State]), Table3[State], ",") = "MA,NY",
"Population in the East States",
CONCATENATEX(VALUES(Table3[State]), Table3[State], ",") = "CA,OR,WA",
"Population in the West States",
HASONEVALUE(Table3[State]),
VALUES(Table3[State]),
"Population by State"
)
The VALUES function returns a list of the distinct values in the column specified.
I've currently got two tables. I have one table with a list of locations as such:
Zagreb (Croatia)
Seattle, WA, USA
New York City, NY
Kazakhstan, Almaty
I also have a master list of 200k cities that looks as such:
Zagreb | Croatia
Seattle | USA
New York City | USA
Almaty | Kazakhstan
The output I want is to add a new column to the first table as below:
Zagreb (Croatia) | Croatia
Seattle, WA, USA | USA
New York City, NY | USA
Kazakhstan, Almaty | Kazakhstan
This updated from a live source that I can't control the data quality from so any solution must be dynamic.
Any ideas appreciated!
One possible approach would be to add a custom column to the first table that searches the string for any cities that appear in the second table City column.
= Table.AddColumn(#"Changed Type", "City",
(L) => List.Select(Cities[City], each Text.Contains(L[Location], _)))
This gives a list of matching cities. Expand that list to get the following:
You can then merge with the Cities table (matching on the City columns from each table) to pull over the Country column.
Here's the full text of my query from the advanced editor:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WikpML0pNUtBwLspPLMlM1FSK1YlWCk5NLCnJSdVRCHfUUQgNdgQL+qWWK0TmF2UrOGeWVOoo+EWCRb0TqxKzM4pLEvN0FBxzchNLKpViYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Location = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Location", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "City", (L) => List.Select(Cities[City], each Text.Contains(L[Location], _))),
#"Expanded City" = Table.ExpandListColumn(#"Added Custom", "City"),
#"Merged Queries" = Table.NestedJoin(#"Expanded City",{"City"},Cities,{"City"},"Cities",JoinKind.LeftOuter),
#"Expanded Cities" = Table.ExpandTableColumn(#"Merged Queries", "Cities", {"Country"}, {"Country"})
in
#"Expanded Cities"
Name the 1st table as "location",including 1 column named "location".
Name the 2nd table as "city",including 2 columns named "city" and "country".
The code is:
let
location = Excel.CurrentWorkbook(){[Name="location"]}[Content],
city = Excel.CurrentWorkbook(){[Name="city"]}[Content],
result = Table.AddColumn(location,"city",each Table.SelectRows(city,(x)=>Text.Contains([location],x[city]))[country]{0})
in
result