Power BI summarized or grouped? How to work out ratio - powerbi

I have spent hours trying to solve and I dont' even know what to search an answer for:
This is my base data table in Power BI
PA
PA FTE
PERSON
PERSON FTE
Anne
1.0
Peter
1.0
Anne
1.0
Simon
1.0
Anne
1.0
James
0.5
Sue
1.0
Andrew
1.0
To help myself, I have created a grouped table, grouped by PA & PA FTE
PA
PA FTE
SUM of PERSON FTE
Anne
1.0
2.5
Sue
1.0
1.0
I want to display on a card the result of TOTAL SUM OF PERSON FTE / TOTAL SUM OF PT FTE
Or 1.75 (which is 3.5/2.0)
If I try and create a measure on my grouped table like this:
MEASURE = SUM([PERSON FTE])/SUM([PA FTE])
I DO NOT GET 1.75

It's better to separate out the 2 tables. When I did, Power BI automatically created a relationship between the 2 tables, which is what was throwing my total off and maybe yours too. Deleting the relationship fixed it.
My result:
My model:
Be sure there is no relationship between the tables.
My DAX:
Total Person FTE = SUM(Person[PERSON FTE])
Total PA FTE = SUM(PA[PA FTE])
Ratio = DIVIDE( [Total Person FTE], [Total PA FTE] )
My Power Query:
// Data
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcszLS1XSUTLUMwCSAaklqUVQXqwOmmRwZm5+Hi5Jr8Tc1GIgbaBnCpYMLkXIOealFKWWw3TGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [PA = _t, #"PA FTE" = _t, PERSON = _t, #"PERSON FTE" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"PA", type text}, {"PA FTE", Int64.Type}, {"PERSON", type text}, {"PERSON FTE", type number}})
in
#"Changed Type"
// PA
let
Source = Data,
#"Removed Other Columns" = Table.SelectColumns(Source,{"PA", "PA FTE"}),
#"Removed Duplicates" = Table.Distinct(#"Removed Other Columns"),
#"Changed Type" = Table.TransformColumnTypes(#"Removed Duplicates",{{"PA FTE", type number}})
in
#"Changed Type"
// Person
let
Source = Data,
#"Removed Other Columns" = Table.SelectColumns(Source,{"PA", "PERSON", "PERSON FTE"})
in
#"Removed Other Columns"

Related

Power BI - power query editor - combine several rows in a table

Given this sample raw table (there are some more columns..):
agg_group
count_%
CHARGED_OFF
1.2
DELINQUENT
1.8
ELIGIBLE
90
MERCHANT_DELINQ
7
NOT_VERIFIED
0
How can I transform this table, to create 2 new columns, using either DAX in Power BI Desktop, or in Power Query?
Desired result:
agg_group
outstanding_principal
ELIGIBLE
90
DELINQUENT
10
Here is a Power Query sample that does this, paste the following into a blank query to see the steps, based on your sample data:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("JcsxDoAgDIXhu3QmRl3UUaVAEyyRoAsh3P8WNnX93v9qhTPs2aPtyTkwMA0zNFPBYiS+H+SiuCqKeToiCm2jyoVZ/lz638uwqHMq/cVMjtAKStw+", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [agg_group = _t, #"count_%" = _t]),
#"Cleaned Text" = Table.TransformColumns(Source,{{"count_%", each Number.FromText(_, "en-US"), type text}}),
#"Added Conditional Column" = Table.AddColumn(#"Cleaned Text", "agg_group_", each if [#"agg_group"] = "ELIGIBLE" then "ELIGIBLE" else "DELINQUENT"),
#"Grouped Rows" = Table.Group(#"Added Conditional Column", {"agg_group_"}, {{"outstanding_principal", each List.Sum([#"count_%"]), type text}}),
#"Renamed Columns" = Table.RenameColumns(#"Grouped Rows",{{"agg_group_", "agg_group"}})
in
#"Renamed Columns"
Result:

PowerBI: Counting days in a range on multiple line for staff absence purposes

I'm working on a staff absence dashboard.
I'd like to know how many staff absences we have on a given day, based on the data below. I would like to be able to create a table of each day, which counts the number of absences on that day. The absence needs to count on the date, which could fall on the start date, end date or in between those.
Full Name Start Date End Date
----------------------------------
Employee D 03/11/2022 05/11/2022
Employee E 03/11/2022 04/11/2022
Employee A 04/11/2022 04/11/2022
Employee B 04/11/2022 06/11/2022
Employee C 04/11/2022 04/11/2022
Employee B 05/11/2022 06/11/2022
Based on the above table, I would expect the following:
Date Count
----------------
03/11/2022 2
04/11/2022 5
05/11/2022 3
06/11/2022 2
I use this formula but the end result isn't counting properly. Could someone help me with the formula?
Count per day = COUNTROWS(FILTER('Staff absence', 'Staff absence'[Absence Start Date]= MIN('Attendance Dates'[Date]) && 'Staff absence'[Absence End Date] >= MAX('Attendance Dates'[Date])))
Best to fix this in your data model. In Power Query join the Absences to the Date table, to create a table with one row for each employee for each day they are absent. Then you can simply count the rows in this table for a given day.
Here's an example:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs0tyMmvTE1VcFHSUTIw1jc01DcyMDICcUzhnFgdJIWuGApNsCt0RJUDc4yMoApjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Full Name" = _t, #"Start Date" = _t, #"End Date" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Full Name", type text}, {"Start Date", type date}, {"End Date", type date}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Days", each List.Dates([Start Date], 1+Number.From([End Date]-[Start Date]), #duration(1, 0, 0, 0))),
#"Expanded Days" = Table.ExpandListColumn(#"Added Custom", "Days"),
#"Renamed Columns" = Table.RenameColumns(#"Expanded Days",{{"Days", "Day Absent"}}),
#"Removed Columns" = Table.RemoveColumns(#"Renamed Columns",{"Start Date", "End Date"})
in
#"Removed Columns"

Power BI -- how to create the desired table for use in a bar chart?

I'm pretty new at Power BI (so forgive my rough terminology), and I'm trying to create a bar chart from some existing financial data. Specifically, I'd like to know how to transform my data. I've looked at DAX and python, and can't quite figure out the right commands.
My existing table looks like the following. The set of categories are arbitrary (not known up front, so can't be hardcoded), same with the set of years.
Category 2002 2003 2004 2005
A $10 $75 $75 $75
B $75 $59 $75 $79
C $15 $32 $13 $5
B $23 $12 $75 $7
C $17 $88 $75 $15
And I want my output table to have the number of rows as the number of unique categories, totaling up the dollar amounts for each year.
Category 2002 2003 2004 2005
A $10 $75 $75 $75
B $98 $71 $150 $86
C $32 $120 $88 $20
What's the best way to roll up the data this way? I intend to use the resulting table to make a composite bar chart, one bar per year.
Thank you!
Avoid Excel-style cross-tables in Power BI. In the PowerQuery Editor transform your table by selecting Categorie and then Unpivot other columns
Back in the designer view you can directly use this data to create a bar chart:
If you like you can also create an aggregated table from your data with the calculated table expression
Aggregated =
SUMMARIZE(
'Table',
'Table'[Category],
'Table'[Year],
"Sum", SUM('Table'[Value])
)
but that's not needed for your purpose.
Here is the full M-Code to achieve your goal: Just change the source step with your source file:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTI0ABLmpkhErE60khOMb2oJl7EEyziD9ID4xkYgljFIDVyLEYhraATXgtBhDiQsLGASQANiYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Category = _t, #"2002" = _t, #"2003" = _t, #"2004" = _t, #"2005" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Category", type text}, {"2002", Int64.Type}, {"2003", Int64.Type}, {"2004", Int64.Type}, {"2005", Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Category"}, "Attribute", "Value"),
#"Renamed Columns" = Table.RenameColumns(#"Unpivoted Other Columns",{{"Attribute", "Year"}}),
#"Grouped Rows" = Table.Group(#"Renamed Columns", {"Category", "Year"}, {{"Total", each List.Sum([Value]), type number}}),
#"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Year]), "Year", "Total", List.Sum)
in
#"Pivoted Column"
If we test it:

Get sum of amount for each account based on aggregation of contracts

I imported the following table which is a join from SQL. The important columns are account_id, contract_id and amount.
I want to get the amount per account (harder than it sounds) the raw import looks something like the table in the description. Neither the contract_id nor the account_id are unique, but I need to group and average the contracts and then sum those averages and group them by account. thanks in advance, really I appreciate the help.
As you can see in the example, contract "a" was repeated therefore we only take it into consideration once
You can also do this in Power Query (Transform):
Remove Duplicates
Group by Account
Select to Aggregate by SUM for the Amount column
M Code
generated by the UI
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUUoEYkMDpVgdDK4RkJkE4prCuclAbASRNQYyU0GyZkqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Account = _t, Contract = _t, Amount = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"Account", Int64.Type}, {"Contract", type text}, {"Amount", Int64.Type}}),
#"Removed Duplicates" = Table.Distinct(#"Changed Type"),
#"Grouped Rows" = Table.Group(#"Removed Duplicates", {"Account"}, {
{"Amount Sold", each List.Sum([Amount]), type nullable number}
})
in
#"Grouped Rows"
If its a second table you want then you can do it like this:
Result =
SUMMARIZE(
'Data',
[Account],
"Amount Sold",
SUMX(
SUMMARIZE(
'Data',
[Account],
[Contract]
),
AVERAGE(Data[Amount])
)
)
Where 'Data' is your input data table.
Update:
Image added with your cod in your comment:
Formatted code:

Add "missing" date rows for respective group and default value in another column in PowerBI?

I'm using PowerBI and looking to summarize (average) data over a period of time, however I realized that my source data doesn't reflect "empty" (zero totals) date values. This are valid and required for accurately aggregating totals over a period of time.
I've created a new date table using the following expression, to create all the dates within the preliminary tables range:
Date_Table = CALENDAR(MIN('SalesTable'[Daily Sales Date]),MAX('SalesTable'[Daily Sales Date]))
However, when trying to create a relationship with the created table and the original SalesTable to fill in the "missing dates" I haven't been successful. If anyone has encountered this a similar issue and has any advice or could point me towards resources to resolve this, I would be greatly appreciative.
I've included an example of my current and expected results below. Thanks!
current:
Item Group
Daily Sales Date
Total
Fruit
January 1
5
Vegetable
January 5
10
expected:
Item Group
Daily Sales Date
Total
Fruit
January 1
5
Fruit
January 2
0
Fruit
January 3
0
Fruit
January 4
0
Fruit
January 5
0
Vegetable
January 1
0
Vegetable
January 2
0
Vegetable
January 3
0
Vegetable
January 4
0
Vegetable
January 5
10
To do this in Power Query as you request, you can create the Date Table in Power Query, then Join it with each group in the Item Group column:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcisqzSxR0lHySswrTSyqVDAEsk2VYnWilcJS01NLEpNyUpFkTYFsQwOl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Item Group" = _t, #"Daily Sales Date" = _t, Total = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"Item Group", type text},
{"Daily Sales Date", type date},
{"Total", Int64.Type}}),
//create a table with a list of all dates for the date range in the table
allDates = Table.FromColumns({
List.Dates(List.Min(#"Changed Type"[Daily Sales Date]),
Duration.Days(List.Max(#"Changed Type"[Daily Sales Date]) - List.Min(#"Changed Type"[Daily Sales Date]))+1,
#duration(1,0,0,0))},type table[Dates=date]),
//group by the item group column
//Then join each subtable with the allDates table
group = Table.Group(#"Changed Type",{"Item Group"},{
{"Daily Sales Date", each Table.Join(_,"Daily Sales Date",allDates,"Dates",JoinKind.RightOuter)}
}),
//Expand the grouped table
#"Expanded Daily Sales Date" = Table.ExpandTableColumn(group, "Daily Sales Date", {"Total", "Dates"}, {"Total", "Dates"}),
//replace the nulls with zero's
#"Replaced Value" = Table.ReplaceValue(#"Expanded Daily Sales Date",null,0,Replacer.ReplaceValue,{"Total"}),
//set proper column order and types
#"Reordered Columns" = Table.ReorderColumns(#"Replaced Value",{"Item Group", "Dates", "Total"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Reordered Columns",{{"Dates", type date}, {"Total", Int64.Type}})
in
#"Changed Type1"
If you wanted to average over the existing date range, you can try this:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcisqzSxR0lHySswrTSyqVDAEsk2VYnWilcJS01NLEpNyUpFkTYFsQwOl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Item Group" = _t, #"Daily Sales Date" = _t, Total = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"Item Group", type text},
{"Daily Sales Date", type date},
{"Total", Int64.Type}}),
//count the number of dates
numDates = Duration.Days(List.Max(#"Changed Type"[Daily Sales Date]) - List.Min(#"Changed Type"[Daily Sales Date]))+1,
//group by Item Group, then average using Sum/Number of dates for each subgroup
#"Grouped Rows" = Table.Group(#"Changed Type", {"Item Group"}, {
{"Average", each List.Sum([Total])/numDates}})
in
#"Grouped Rows"
And there are numerous other ways of accomplishing what you probably require.