here is the deal... (actually I've solved it but I am sure there is an easier way)
my table is :
Department ID
Criteria 1
Criteria 2
Criteria 3
DEP 001
4
5
3
DEP 002
5
5
5
DEP 003
3
4
5
DEP 004
2
3
5
DEP 001
4
4
5
DEP 003
1
2
4
DEP 002
2
2
4
DEP 003
3
5
2
DEP 002
5
2
5
DEP 005
4
3
1
DEP 001
1
5
3
DEP 002
2
1
2
DEP 003
4
2
5
DEP 005
3
4
1
DEP 002
5
5
4
DEP 005
1
2
2
DEP 001
2
3
1
DEP 002
3
1
5
I am trying to find the top 1 average in each criteria.
I've used unpivot, creating new table and then calculated column to find the result.
First Unpivoting
let
Source = YourTableName,
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Department ID", type text}, {"Criteria 1", Int64.Type}, {"Criteria 2", Int64.Type}, {"Criteria 3", Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Department ID"}, "Attribute", "Value")
in
#"Unpivoted Other Columns"
Second calculated Table :
Max Avg =
VAR _cr1 =
SUMMARIZE (
'YourTableName (unpivoted)',
'YourTableName (unpivoted)'[Department ID],
'YourTableName (unpivoted)'[Attribute],
"Criteria Avg",
CALCULATE (
AVERAGE ( 'YourTableName (unpivoted)'[Value] ),
ALLEXCEPT (
'YourTableName (unpivoted)',
'YourTableName (unpivoted)'[Department ID],
'YourTableName (unpivoted)'[Attribute]
)
)
)
RETURN
_cr1
and finally the calculated rank column and filter by 1
RANK =
RANKX (
FILTER (
'Max Avg',
'Max Avg'[Attribute] = EARLIER ( 'Max Avg'[Attribute])
),
'Max Avg'[Criteria Avg],
,
DESC,
DENSE
)
expected result is :
Can you suggest an easier way ?
This is PBix file where you can work on to suggest a faster way
Only by creating a couple of complicated measures. BTW, I'd always reshape data in PQ rather than DAX. Here is an alternative:
Create a criteria table.
Create a measure:
Attribute =
VAR a = ADDCOLUMNS(
SUMMARIZE(YourTableName, YourTableName[Department ID]) ,
"#crit1", CALCULATE(AVERAGE(YourTableName[Criteria 1])),
"#crit2", CALCULATE(AVERAGE(YourTableName[Criteria 2])),
"#crit3", CALCULATE(AVERAGE(YourTableName[Criteria 3]))
)
return
SWITCH(SELECTEDVALUE(Criteria[Criteria]),
"Criteria 1", MAXX(a, [#crit1]),
"Criteria 2", MAXX(a, [#crit2]),
"Criteria 3", MAXX(a, [#crit3])
)
Add to a table visual
Result
Related
I have a rather large table in PowerBI that looks as follows:
Date1
ID1
ID2
Date2
Amount1
Amount2
Amount3
04.02.2022
1234
12
04.02.2022
5
3
8
04.02.2022
1234
13
04.02.2022
5
3
8
04.02.2022
1235
14
04.02.2022
6
3
9
06.02.2022
1234
10
06.02.2022
20
23
46
06.02.2022
1238
11
06.02.2022
20
23
46
06.02.2022
1238
14
06.02.2022
26
23
49
As in the case above, if e.g. 05.02.2022 is missing, I would like my end result to look like
Date1
ID1
ID2
Date2
Amount1
Amount2
Amount3
04.02.2022
1234
12
04.02.2022
5
3
8
04.02.2022
1234
13
04.02.2022
5
3
8
04.02.2022
1235
14
04.02.2022
6
3
9
05.02.2022
1234
12
05.02.2022
5
3
8
05.02.2022
1234
13
05.02.2022
5
3
8
05.02.2022
1235
14
05.02.2022
6
3
9
06.02.2022
1234
10
06.02.2022
20
23
46
06.02.2022
1238
11
06.02.2022
20
23
46
06.02.2022
1238
14
06.02.2022
26
23
49
Which means that everything from 04.02.2022 is copy pasted, just with a new date, 05.02.2022.
There are also cases where no data is available for 2 or 3 days, so in those instances I would need the all data from the last known date, until we have data again.
Does someone know how to implement this in PowerBI?
Thank you!
The following should work for you. I have named your sample data query as Table.
Create a new query and paste in the following code. This new query refers to your sample data query named Table so you will have two queries.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDRMzDSMzIwMlKK1QFyTVG5ZghuLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
#"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Date"}, Table, {"Date1"}, "Table", JoinKind.LeftOuter),
#"Added Custom" = Table.AddColumn(#"Merged Queries", "Count", each if Table.RowCount([Table]) > 0 then [Table] else null),
#"Filled Down" = Table.FillDown(#"Added Custom",{"Count"}),
#"Added Custom1" = Table.AddColumn(#"Filled Down", "Custom", each if Table.RowCount([Table]) > 0 then [Table] else Table.ReplaceValue([Count],[Count]{0}[Date1],[Date],Replacer.ReplaceValue,{"Date1", "Date2"})),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Table", "Count"}),
#"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Date1", "ID1", "ID2", "Date2", "Amount1", "Amount2", "Amount3"}, {"Date1", "ID1", "ID2", "Date2", "Amount1", "Amount2", "Amount3"}),
#"Removed Columns1" = Table.RemoveColumns(#"Expanded Custom",{"Date"})
in
#"Removed Columns1"
If you need to fill out more dates, then just change the date range in step 1 which you should be able to auto generate depending on your data. Mine looks like this.
Need help in creating measures that will reflect the actual count of rows in the table when filtered.
Example:
ID
RankC
RankA
Avg Diff
RankC_count
RankA_count
Avg Diff_count
1000
AAA
XYZ
+01.00 to +01.25
5
6
4
1001
AAA
ZY1
+01.5.00 to +01.75
5
1
5
1002
AAB
XYZ
+01.5.00 to +01.75
3
6
5
1003
AAB
ZY2
+01.5.00 to +01.75
3
1
5
1004
AAB
XYZ
+01.00 to +01.25
3
6
4
1005
AAA
XYZ
+01.00 to +01.25
5
6
4
1006
AAA
ZY3
+01.00 to +01.25
5
1
4
1007
AAC
XYZ
+01.25.00 to +01.5
1
6
2
1008
AAA
ZY4
+01.25.00 to +01.5
5
2
2
1009
AAZ
ZY4
+01.5.00 to +01.75
1
2
5
1010
ABY
XYZ
+01.5.00 to +01.75
1
6
5
The last 3 columns represent the count of each entry. If I use the measure such as below, it provides the correct count. However, when I use in the visual, filtering by ID, say ID 1000, I want it to show line 1 with 5,6, and 4 on the counts, instead of all 1.
Questions:
Is there any measure to give me the correct result? say summarize the table first then do a lookup?
is creating a column the only choice? I cannot create columns since I need 1000 of these calculated columns. whereas using measure, I can create 1000 in one go.
Thanks for any help.
AverageDiff_Count =
CALCULATE (
COUNTROWS (
FILTER ( '28Jun_1973', [Average Diff] = '28Jun_1973'[Average Diff] )
)
)
The ALL function is useful here. It removes filter context so that it uses the whole table instead of just the part in the current filter context.
AvgDiff_Count =
VAR CurrAvgDiff = SELECTEDVALUE ( '28Jun_1973'[Avg Diff] )
RETURN
COUNTROWS (
FILTER ( ALL ( '28Jun_1973' ), '28Jun_1973'[Avg Diff] = CurrAvgDiff )
)
I want to numerate the occurances of a specific column in my table. The best way I have thought to do this is to count the rows of a filtered table. So, my WorkOrders table looks like this:
WO# Date CompCode Serial#
001 1/1/2021 100 A
001 1/1/2021 101 A
002 1/2/2021 100 B
003 2/1/2021 100 A
004 2/2/2021 100 B
005 2/15/2021 101 A
006 3/1/2021 102 A
006 3/1/2021 100 A
I want to create a new column that numerates the occurance on the CompCode by Serial#. There is no gaurantee that the data is sorted by date. So, I tried to count the rows of a filtered table using this formula:
COMP_OCCURANCE =
CALCULATE(
COUNTROWS(WorkOrders),
Serial# = Serial#,
Date <= Date,
CompCode = CompCode
)
I assumed that would work but it does not. The desired result would look like this:
WO# Date CompCode Serial# COMP_OCCURANCE
001 1/1/2021 100 A 1
001 1/1/2021 101 A 1
002 1/2/2021 100 B 1
003 2/1/2021 100 A 2
004 2/2/2021 100 B 2
005 2/15/2021 101 A 2
006 3/1/2021 102 A 1
006 3/1/2021 100 A 3
Thanks in advance for the help.
Try this:
COMP_OCCURANCE =
VAR CurrentSerial = WorkOrders[Serial #]
VAR CurrentDate = WorkOrders[Date]
VAR CurrentCode = WorkOrders[CompCode]
VAR Result =
CALCULATE (
COUNTROWS ( WorkOrders ),
WorkOrders[Serial #] = CurrentSerial,
WorkOrders[CompCode] = CurrentCode,
WorkOrders[Date] <= CurrentDate,
REMOVEFILTERS ( WorkOrders )
)
RETURN
Result
Screenshot - https://ibb.co/Bj5xrFS
For this type of calculation, you must need to rely on a column for sorting/ordering your data. As you cannot use Date in this case, I think you can go for column "WO#" this case. If this is acceptable, you can try this below measure for your expected output-
COMP_OCCURANCE =
CALCULATE(
COUNT(your_table_name[WO#]),
FILTER(
ALL(your_table_name),
your_table_name[WO#] <= MIN(your_table_name[WO#])
&& your_table_name[CompCode] = MIN(your_table_name[CompCode])
&& your_table_name[Serial#] = MIN(your_table_name[Serial#])
)
)
Output-
I have the leave data of a company.
Here is the sample data:
STAFF PL CL ML SP LWP TL Month
A 1 2 0 0 6 9 April
B 14 0 4 0 0 18 April
A 1 2 0 0 1 4 May
B 1 0 4 0 0 5 May
A 1 2 0 0 2 5 June
B 2 0 4 0 0 6 June
I want to transform this data into a table structure like this-
Here is the sample data:
Types of Leave Count Month
ML 89 4
CL 114 4
LWP 17 4
PL 135 4
SP 89 4
ML 89 5
CL 114 5
LWP 17 5
PL 135 5
SP 89 5
ML 89 6
CL 114 6
LWP 17 6
PL 135 6
SP 89 6
Can it be possible using SelectColumns, Summarizecolumn dax functions?
I tried --
SUMMARIZE(Table1, Table1[CL],Table1[LWP],Table1[ML],Table1[PL],"CL2", SUM(Table1[CL]), "ML2", SUM(Table1[ML]), "LW2P",SUM(Table1[LWP]), "P2L", SUM(Table1[PL]))
It just gave me weird results.
Unpivot the data after removing the name and the staff columns. Then convert the month name to the month number and build a pivot table.
The screenshot shows the result in Excel, but the same can be done in Power BI.
Here is the M code for the query as it was recorded when I clicked the ribbon icons:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"STAFF", type text}, {"PL", Int64.Type}, {"CL", Int64.Type}, {"ML", Int64.Type}, {"SP", Int64.Type}, {"LWP", Int64.Type}, {"TL", Int64.Type}, {"Month", type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"STAFF", "TL"}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Removed Columns", {"Month"}, "Attribute", "Value"),
#"Renamed Columns" = Table.RenameColumns(#"Unpivoted Other Columns",{{"Attribute", "Type of Leave"}, {"Value", "Count"}}),
#"Added Custom" = Table.AddColumn(#"Renamed Columns", "Custom", each Date.FromText([Month]&"1 2017")),
#"Extracted Month" = Table.TransformColumns(#"Added Custom",{{"Custom", Date.Month}}),
#"Removed Columns1" = Table.RemoveColumns(#"Extracted Month",{"Month"}),
#"Renamed Columns1" = Table.RenameColumns(#"Removed Columns1",{{"Custom", "Month"}})
in
#"Renamed Columns1"
I have a table in PowerBI that looks like this:
Date StoreID Car Sales <Row Num (for explanation only)>
1/1/2017 1 0 1
1/2/2017 1 0 2
1/3/2017 1 0 3
1/4/2017 1 20 4
1/5/2017 1 13 5
1/6/2017 1 0 6
1/7/2017 1 31 7
1/4/2017 2 0 8
1/5/2017 2 0 9
1/6/2017 2 7 10
1/7/2017 2 0 11
1/8/2017 2 10 12
What I am trying to do is create a measure that will calculate Car Sales by day (so on a line chart with Date on the x-axis), but eliminate the rows/records with 0's until the first Date that has a Sales value. In other words, I want to eliminate rows 1, 2, and 3, but not eliminate row #6, because that is a legitimate day where there were no cars sold. I also want to do this for every StoreID, so I want to eliminate rows 8 and 9, but not 11. Is there any way to come up with a measure/column (or other means) that will accomplish this in PowerBI?
You can group by StoreID, and then transform each column with: sort by [Date], then use Table.Skip to remove rows where [Car Sales] is 0. (Sorting by [Date] might not seem necessary, but group by can change ordering.) Then expand out the grouped tables.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type datetime}, {"StoreID", Int64.Type}, {"Car Sales", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"StoreID"}, {{"Grouped", (grouped) => let
#"Sorted Rows" = Table.Sort(grouped,{{"Date", Order.Ascending}}),
SkipNoCarSales = Table.Skip(#"Sorted Rows", each [Car Sales] = 0)
in
SkipNoCarSales, type table}}),
#"Expanded Grouped" = Table.ExpandTableColumn(#"Grouped Rows", "Grouped", {"Car Sales", "Date"}, {"Car Sales", "Date"}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded Grouped",{"Car Sales", "StoreID", "Date"})
in
#"Reordered Columns"