How to add columns after unpivoting in Power Bi? - powerbi-desktop

I have a dataset with data separated by month in every column. I selected "unpivot all other columns" and selected the fields before the month columns. So now I have my data aggregated by all the months in one column and all the values in another column. Problem is that every month, I need to add a new column. I clicked refresh data, and the new column does not appear. Looking in the Advanced Editor, it shows the file having 70 columns, when it should be 71. I'm not sure why it's not even reading the new column.
Sample Data (Before)
Jan 21 Feb 21 Mar 21 Apr 21
Sample Data (After)
Month Value
Jan 21
Feb 21
Mar 21
Apr 21
Problem arises when I add May - refresh the data, I do not see it in the Month column.
I'm not sure if it's because I add a custom column after unpivoting the other ones (non-month columns). This custom column is to change the format of the newly created "Month" column to date in MM-DD-YY format.
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Removed Columns", [insert non-month columns here]
#"Added Custom" = Table.AddColumn(#"Renamed Columns", "Date", each Date.FromText("01/" & Text.Start([Month],3) & "/20" & Text.End([Month],2))),

Related

How to use mid function in Power query

hello help me to use mid function to get the date(October 15 2022) in column1 all rows, from column 33 (AUC as on October 15 2022) in power query.
Potentially you want this which pulls the date from the first row in [Column33]
TextDate = Date.FromText( Text.Split( #"PriorStepNameHere"{0}[Column33],"as on "){1}),
#"Added Custom" = Table.AddColumn(#"PriorStepNameHere", "ExtractedDate", each TextDate)
EDIT:
try adding a column ... custom column with this code (replacing #"PriorStepNameHere" with your actual prior step name)
= Date.FromText( Text.Split( #"PriorStepNameHere"{0}[Column33],"as on "){1})

Count date range based on criteria

Let´s suppose I have generated a report with dates (day/month/year) when soccer teams won titles. This is how the report is going to look like:
Area
Team
Champions League
Europe League
England
Chelsea
27/01/2021
15/01/2021
Spain
Real Madri
27/02/2021
20/01/2021
Spain
Barcelona
18/02/2021
France
PSG
27/03/2021
27/02/2021
My objective here is going to count how many titles each area won per month. So, this is how my desired output looks like:
Area
January
February
March
England
2
Spain
1
2
France
1
1
What I tried to do was the following (for January and England):
=COUNTIFS(Table[[#All],[Champions League]:[Europe League]],">01/01/2021",Table[[#All],[Champions League]:[Europe League]],"<31/01/2022",Table[[#All],[Area]],"=England")
However, my output using this formula is "#VALUE!". Can you please help trying to figure out what I am doing wrong?
You may try something like this as shown in image below, so I have used the incredibly versatile SUMPRODUCT Function to achieve the expected output
Formula used in cell B8 Fill Down & Fill Right
=SUMPRODUCT(($A8=$A$2:$A$5)*(B$7=TEXT($C$2:$D$5,"mmmm"))*($C$2:$D$5<>""))
To hide the zeros, you may custom format the cells by pressing CTRL 1 --> Format Cells --> Number Tab --> Category --> Custom --> Type --> 0;;
EDIT
Approach Using Power Query
let
Source = Excel.CurrentWorkbook(){[Name="Table39"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Area", type text}, {"Team", type text}, {"Champions League", type date}, {"Europe League", type date}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Area", "Team"}, "Attribute", "Value"),
#"Extracted Month Name" = Table.TransformColumns(#"Unpivoted Other Columns", {{"Value", each Date.MonthName(_), type text}}),
#"Grouped Rows" = Table.Group(#"Extracted Month Name", {"Area"}, {{"All", each _, type table [Area=nullable text, Team=nullable text, Attribute=text, Value=text]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([All],"Index",1,1)),
#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Value", "Index"}, {"Value", "Index"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"All"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Value]), "Value", "Index", List.NonNullCount)
in
#"Pivoted Column"
Using Power Query will be dynamic, one time operation and every time add new data to the original source the imported table from Power Query gets updated within few by a single refresh, please follow the steps
• First select any cell within the range,
• Then from Data Tab under Get & Transform Data, Click From Table/Range,
• A pop up shall appear, check mark the My table has headers and press Ok
• Data gets loaded into Power Query Editor
• Change the name of the table from Query Settings in the right hand under Properties with CountTbl
• Now select any column and press CTRL A
• If the Changed Type Step is already showing in Applied steps then you don't have to follow this, please goto the next step
From Transform Tab --> Click Detect Data Type
• Now you will find the dates are still showing as Time Stamps therefore select both Champions League & Europa League Columns and from Transform Tab --> Click Data Type from Any to Date only, you shall get a message asking whether to Replace the current step or to add new step, you need to select the Replace
• Well data types changed, now select the Area and Team Columns and press Right Click Unpivot Other Columns also you may find the option from Transform Tab --> Unpivot Columns dropdown and select Unpivot Other Columns
• Next, select the Value column showing dates --> Goto Transform Tab --> From Date & Time Column Group --> Click Date --> Month --> Name Of Month
• Again select the Area Column --> From Transform Tab --> Group By --> Give any fancy name to the New Column Name --> Operation --> All Rows and press OK
• Now goto Add Column Tab --> Click Custom Column and enter this following Formula or the M-Code in custom column formula space
Table.AddIndexColumn([All],"Index",1,1)
press Ok
• So from the custom column newly created, click on the dropdown, and uncheck the box use original column name as prefix and select the columns Value and Index and press Ok
• Remove the column which we created by Group i.e. All --> Select the column and press delete key from your keyboard
• Now select the value column which will be showing the month name and goto Transform Tab and click Pivot Column
• There Values Column needs to be Index and in Advanced Options you to select Count (Not Blank) and press OK
• You shall see the expected output has been achieved!!!
• From Home Tab Click Close & Load To --> Import Data --> Table --> You can either select Existing Worksheet (enter the cell reference where you want to place the imported table) or New Worksheet and press Ok
IMPORT DATA
• Done Full_Explanation Power_Query
COUNTIFS doesn't like your dissimilar sized criteria ranges.
You would be better served setting your data up in a tabular format like so:
Area
Team
Championship
Date
England
Chelsea
Champions League
1/27/2021
England
Chelsea
Europe League
1/15/2021
Spain
Real Madri
Champions League
2/27/2021
Spain
Real Madri
Europe League
1/20/2021
Spain
Barcelona
Europe League
2/18/2021
France
PSG
Champions League
3/27/2021
France
PSG
Europe League
2/27/2021
You get your data into this format easily by using Power Query. Simply load the data in, select the two date columns, and Unpivot.
And then you could use a simple Pivot Table to display the data in your preferred format:
To turn the full date to just the Month name select one cell > right-click > Group > by Month

Consolidate entries in Excel

I'm attempting to consolidate the unique dates that some students do their homework. Goal is to get the unique number of entries of date&name (i.e. multiple entries of one person on same date counts as one), and ideally the output can be the following. I thought about using arrays or pivot table but I can't think of any non-manual way to do this. Thanks a lot (pardon my poor formatting...).
(Note: the actual problem involves a wide range of dates and >100 names).
Input
Date Name Quan
10/22/2019 Amy 4
10/10/2019 Amy 3
10/23/2019 Amy 1
10/23/2019 Amy 3
10/10/2019 Amy 5
1/31/2011 Cathy 5
1/31/2011 Cathy 2
10/23/2019 Cathy 1
1/31/2011 Cathy 4
1/31/2011 Cathy 5
Output
Date Name Quan
10/23/2019 Amy 4
10/22/2019 Amy 4
10/10/2019 Amy 8
10/23/2019 Cathy 1
1/31/2011 Cathy 16
If you have O365 with dynamic arrays, you can use all formulas
To get the unique list of Date/Name, sorted in the order you show:
eg: F1: =SORT(UNIQUE(INDEX(Table1,SEQUENCE(ROWS(Table1)),{1,2})),{2,1},{1,-1})
Results will spill to the appropriate rows
To get the sums:
eg: H2: =SUMIFS(Table1[Quan],Table1[Date],F2,Table1[Name],G2)
Note that I used a Table and structured references, but you can use regular range references if you prefer. Structured references have the ability of automatically expanding/contracting if you add/remove data from your table.
Note:
If you don't have the latest functions, you can still use formulas:
To create a unique list, you'll need a helper column in, eg: K
K2: =IFERROR(INDEX(Table1[Date]&Table1[Name],MATCH(0,COUNTIF($K$1:K1,Table1[Date]&Table1[Name]),0)),"")
Fill down until Blanks
Then, for the Date Column:
L2: =--LEFT(K2,5)
Name column:
M2: =MID(K2,6,99)
And use SUMIFS as before to get the SUM.
You'll need to sort using the Data/Sort tab.
Another method is by using Power Query aka Get & Transform (available in Excel 2010+) to
Group By Date and Name
Aggregate with Sum
MCode
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Name", type text}, {"Quan", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Date", "Name"}, {{"Quant", each List.Sum([Quan]), type number}}),
#"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"Date", Order.Descending}, {"Name", Order.Ascending}})
in
#"Sorted Rows"
I assume you have three rows: Date, Name, and Quan?
If that's the case, you can try using the CONCATENATE formula to combine Date and Name, then use the new "Date & Name" row and the "Quan" row to make pivot table.
CONCATENATE result should look like this
Pivot table will look for the same "Date & Name" and give you the "Quan" sum you want.
If you use office 2016 or higher (or you have installed PowerQuery-Addin) and you have a lot of Datarow. Group the Data with PowerQuery and without any formula

How to add Hour wise data to stacked chart in powerbi

I have sensor data that storing in table storage and this data is used as the data source for Powerbi. The data contains 6 data for a DateTime hour. For example 50 for 2019-10-24 12:10 Am, 65 for 2019-10-24 12:20 Am etc
I want to show the graph as the following format where Y axis shows 10th mint, 20th mint to 50th mint of an hour
Is it possible to create such graph in PowerBi? I am new to PowerBi
I tried one and got the following result
The query is like
let
Source = AzureStorage.Tables("https://xxxxx.table.core.windows.net"),
AveragedResult1 = Source{[Name="AveragedResult"]}[Data],
#"Expanded Content" = Table.ExpandRecordColumn(AveragedResult1, "Content", {"DayID", "DeviceDayID", "Leq", "max", "min", "date"}, {"Content.DayID", "Content.DeviceDayID", "Content.Leq", "Content.max", "Content.min", "Content.date"}),
#"Filtered Rows" = Table.SelectRows(#"Expanded Content", each ([PartitionKey] = "2019-10-21" ))
in
#"Filtered Rows"
create a 10 min bin.. and then have that column as your y axis and hour for x axis.

DAX Measure to calculate number of lost days in different year from total number of days

I am trying to calculate number of days for particular year based on calendar table that i have created.
For Example: I have 3 columns.
Event, number of days and Date when this event started
Event DaysLost
Injury 30 25/12/2016
Injury 588 06/08/2012
Days in 2016 - 6
Days in 2017 - 24
For the second case:
Days in 2012 - 146
Days in 2013 - 365
Days in 2014 - 77
Now for above case there are only 6 days which need to be counted in 2016 and the rest of the days should automatically be counted in 2017. But i cannot figure out how to do it.
In my output i would like to put years in one column and days lost for year in front of that particular year.
I have a calendar table and i want sum of days to populate for a particular year.
I tried calculating it by getting end date, by adding number of days to First start date and then if days were more that remaining days in that year. subtract remaining days from total days and remaining days should move to next year. But i cannot figure out how to keep adding days for next years if days extends for many years and list them after words.
Sept 4, 2017
Please see the excel solution below
Excel solution of the problem
0) Importing the data from your Excel screenshot into Power BI results in this.
1) Create a new column in that table using the following formula for end date (to help with future formulas).
EndDate = Injuries[First Start Date] + Injuries[Days]
You stated that you have a calendar table, so you can skip to step 3
2) Create a new table by clicking on Modeling -> New Table and entering the following formula. This gives a single column table with a list of years.
Years = GENERATESERIES(2000, 2020, 1)
3) Create another new table using the following formula. This gives a table with all of the fields from the initial data table crossjoined with the Year table that was just created. The formula also filters the resulting table to only return rows where the value in the Year column is between the First Start Date and the First Start Date plus Days. To learn more about the CROSSJOIN function, check of the documentation here.
InjuriesByYear = FILTER(
CROSSJOIN(Years, Injuries),
Years[Year] >= Injuries[First Start Date].[Year] &&
Years[Year] <= Injuries[EndDate].[Year]
)
4) Create relationships from the InjuriesByYear table back to the initial data table and the Year table. This will help facilitate nicer reporting efforts.
5) In the InjuriesByYear table, create a new column by clicking on Modeling -> New Column and entering the following formula. The first IF checks if all of the days lost are in a single year. The second IF handles when the days are spread across multiple years, with the True clause handling the first year, and the False clause handling all other years.
DayPerYear = IF(
InjuriesByYear[Year] = InjuriesByYear[First Start Date].[Year] && InjuriesByYear[Year] = InjuriesByYear[EndDate].[Year], InjuriesByYear[Days],
IF(
InjuriesByYear[Year] = InjuriesByYear[First Start Date].[Year], DATEDIFF(InjuriesByYear[First Start Date], DATE(InjuriesByYear[First Start Date].[Year], 12, 31), DAY),
DATEDIFF(DATE(InjuriesByYear[Year], 1, 1), MIN(InjuriesByYear[EndDate], DATE(InjuriesByYear[Year], 12, 31)), DAY) + 1
)
)
6) To test it all out, create a pivot table as configured in below. Following these steps, the pivot table should match your Excel solution.
This is a Power Query based approach...
I started with this:
Then I added a custom column by clicking the Add Column tab and Custom Column button and completing the pop-up window like this:
...and clicking OK.
Then I changed the type for that new column by selecting it and then clicking the Transform tab and then Data Type and Date.
Then I added another custom column, completing the pop-up like this:
Then I added another custom column, completing the pop-up like this:
Then I added yet another custom column, completing the pop-up like this:
Then I expanded that last column I added by clicking on the at the top of the column and Expand to New Rows.
Then I added a final custom column, completing the pop-up like this:
Finally, I grouped by the Event, DaysLost, Started, and Year columns and summed the DaysLostForYear column by clicking the Transform tab and Group By button and completing the pop-up like this:
I end up with this:
You might want a different grouping, but this should get you close. It shows how many days were lost in the years associated with each instance of an injury's total days lost. For instance, the first injury, which was 30 days in duration, started on 12/25/2016: 7 of those days occurred in 2016 and 23 in 2017. The second injury was 588 days, started on 8/6/2012: 148 days were in 2012, 365 in 2013, and 75 in 2014.
Note that I count the started date as a lost day.
Note also that I account for leap years.
I hope this helps.
Here's the query code:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Event", type text}, {"DaysLost", Int64.Type}, {"Started", type date}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Ended", each Date.AddDays([Started],[DaysLost]-1)),
#"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Ended", type date}}),
#"Added Custom3" = Table.AddColumn(#"Changed Type1", "DaysYearStarted", each Number.From(Date.From(Text.From(Date.Year([Started]))&"/12/31")-[Started])+1),
#"Added Custom4" = Table.AddColumn(#"Added Custom3", "DaysYearEnded", each Number.From([Ended]-Date.From(Text.From(Date.Year([Ended])-1)&"/12/31"))),
#"Added Custom5" = Table.AddColumn(#"Added Custom4", "Year", each List.Numbers(Date.Year([Started]),Date.Year([Ended])-Date.Year([Started])+1)),
#"Expanded Custom" = Table.ExpandListColumn(#"Added Custom5", "Year"),
#"Added Custom1" = Table.AddColumn(#"Expanded Custom", "DaysLostForYear", each if [Year]=Date.Year([Started]) then [DaysYearStarted] else
if [Year]=Date.Year([Ended]) then [DaysYearEnded] else
if Date.IsLeapYear([Year]) then 366 else 365),
#"Grouped Rows" = Table.Group(#"Added Custom1", {"Event", "DaysLost", "Started", "Year"}, {{"DaysLostForYear", each List.Sum([DaysLostForYear]), type number}})
in
#"Grouped Rows"

Resources