I have the following data:
Country Sales
1 20
1 30
1 10
2 25
2 80
3 200
3 4
4 20
5 30
I want to have top 2 country sales summed up and the rest
Country Sales
2 105
3 204
Others 110
I want to display the summed up data in a pie chart.
Rank Sales =
RANKX(ALL(Table[Country]),CALCULATE(sum(Table[Sales])))
What should I do ahead? The if condition is giving an error:
Rank Display =
IF('DAX Measures'[Rank Sales]>2,"Other",'Table'[country])
Please create a new table, and paste this code:
CategorizationByRank =
VAR Summary_01 =
ADDCOLUMNS (
VALUES ( 'Table'[Country] ),
"Total", CALCULATE ( SUM ( 'Table'[Sales] ) )
)
VAR Summary_02 =
ADDCOLUMNS ( Summary_01, "Ranking", RANKX ( Summary_01, [Total] ) )
VAR TablesRankLess_And_Equal_To_2 =
FILTER ( Summary_02, [Ranking] <= 2 )
VAR TablesRankGreater_Than_2 =
ROW (
"Country", "Others",
"Total", SUMX ( FILTER ( Summary_02, [Ranking] > 2 ), [Total] ),
"Ranking", 3
)
VAR UnionAllRecords =
UNION ( TablesRankGreater_Than_2, TablesRankLess_And_Equal_To_2 )
RETURN
UnionAllRecords
And It produces this result:
Then you can put the fields directly into a pie chart, see the picture:
Related
I have a table with data. I want to find the average by Criteria 1 for each Department ID (Department IDs are repeated) and output the value of the Department ID that has the maximum average. How can I do this? [I attach a table with data.]
Department ID
Criteria 1
DEP 001
4
DEP 002
2
DEP 003
1
DEP 004
5
DEP 001
3
DEP 003
2
DEP 002
4
DEP 001
3
DEP 004
2
DEP 001
1
DEP 002
3
DEP 003
4
Average(DEP 001) = (4+3+3+1)/4 = 11/4 = 2.75
Average(DEP 002) = (2+4+3)/3 = 9/3 = 3
Average(DEP 003) = (1+2+4)/3 = 7/3 = 2.3
Average(DEP 004) = (5+2)/2 = 7/2 = 3.5
MAX(Average(DEP 001), Average(DEP 002), Average(DEP 003), Average(DEP 004)) = 3.5
Result = DEP 004
first let's create the average table
Modelling --> New Table
average table =
ADDCOLUMNS (
SUMMARIZE ( YourTableName, YourTableName[Department ID ] ),
"avg",
CALCULATE (
AVERAGE ( YourTableName[Criteria 1] ),
ALLEXCEPT ( YourTableName, YourTableName[Department ID ] )
)
)
then let's create the card which shows the max
Maximum Average =
VAR _max =
MAX ( 'average table'[avg] )
RETURN
LOOKUPVALUE ( 'average table'[Department ID ], 'average table'[avg], _max )
but, like your first post, if you have more than 1 criteria, you should unpivot and find a solution after the unpivoting ;)
Here is a measure that does this directly without adding bloat in your model:
ID of Max Avg =
VAR _tbl =
SUMMARIZE (
'Table' ,
'Table'[Department ID] ,
"Avg" , AVERAGE ( 'Table'[Criteria 1] )
)
VAR _top =
TOPN (
1 ,
_tbl ,
[Avg] ,
DESC
)
RETURN
CALCULATE (
SELECTEDVALUE ( 'Table'[Department ID] ) ,
_top
)
Here the measure is used in a Card visual using your sample data:
CALCULATE(
SELECTEDVALUE(tbl[Department ID])
,FILTER(
VALUES(tbl[Department ID])
,CALCULATE(AVERAGE(tbl[Criteria 1]))=MAXX(
VALUES(tbl[Department ID])
,CALCULATE(Average(tbl[Criteria 1]))
)
)
)
I'm new at power BI and i've a model with table that call DIVUCHIM_MONE.
I want to get the last [Id] value for the [work_order].
The same [work_order] can be appear several time, the [Id] not.
For exmple:
Id work_order
1 215353
2 215325
3 215325
4 215353
5 215221
6 215231
7 215221
8 215325
9 215353
10 215231
I expected to get:
Id work_order Id_Before
1 215353 0
2 215325 0
3 215325 2
4 215353 1
5 215221 0
6 215231 0
7 215221 5
8 215325 3
9 215353 4
10 215231 6
Please try the following measure
_id =
VAR _currentlyVisibleID =
MAX ( 'Fact'[Id] )
VAR _immediatelyPrecedingByWO =
CALCULATE (
CALCULATE ( MAX ( 'Fact'[Id] ), 'Fact'[Id] < _currentlyVisibleID ),
ALLEXCEPT ( 'Fact', 'Fact'[work_order] )
)
RETURN
_immediatelyPrecedingByWO+0
Edit
Calculated Column
Column =
VAR _currentlyVisibleID =
CALCULATE(MAX ( 'Fact'[Id] ))
VAR _immediatelyPrecedingByWO =
CALCULATE (
CALCULATE ( MAX ( 'Fact'[Id] ), 'Fact'[Id] < _currentlyVisibleID ),
ALLEXCEPT ( 'Fact', 'Fact'[work_order] )
)
RETURN
_immediatelyPrecedingByWO+0
I have a simple problem. My DAX measure does not seem to be working correctly when I filter for non-existing values. Here are some details:
Table:
Column1: A,A,A,A,A,B,B,B,B
Column2: 1,2,3,4,5,1,2,3,5
Measure = calculate(countrows(table), allexcept(column1))
Card Visual returns correct row count when I filter by column1 (any value in filtering pane)
However it returns wrong row count when I filter by column2 = "4" and Column1 = "B" (in filtering pane). It seems that it should ingore filtering by column2 and it does except when I specifically filer for value = "4". It gives "blank" result value in a card visual then.
Any ideas why?
Here's the screen. I would like to populate that blank cell with "4" (in a singe-table data model.enter image description here
In your case you dont need to add allexcept in your measure. Below code would be fine.
TestMeasure = countrows(Test_Data)
PFB screenshot
I am hoping that you have a data model as following
table name _dim1
colA
A
B
C
table name _dim2
colB
1
2
3
4
5
table name _fact
colA
colB
A
1
A
2
A
3
A
4
A
5
B
1
B
2
B
3
B
5
C
2
C
3
If you have this you can reach where you need by using following measures
Measure3 =
CALCULATE ( COUNTROWS ( _fact ), ALL ( _dim2[colB] ), VALUES ( _fact[colA] ) )
Measure9 =
VAR _1 =
MAX ( _dim2[colB] )
VAR _2 =
CALCULATE (
MAXX (
FILTER ( _dim2, _dim2[colB] <= _1 ),
LASTNONBLANKVALUE ( _dim2[colB], [Measure3] )
),
ALL ( _dim2[colB] )
)
RETURN
_2
Measure10 =
VAR _1 =
MAX ( _dim2[colB] )
VAR _2 =
CALCULATE (
MAXX (
FILTER ( _dim2, _dim2[colB] > _1 ),
FIRSTNONBLANKVALUE ( _dim2[colB], [Measure3] )
),
ALL ( _dim2[colB] )
)
RETURN
IF ( ISBLANK ( [Measure9] ) = TRUE (), _2, [Measure9] )
I don't think you can reach here from a single table like following
colA
colB
A
1
A
2
A
3
A
4
A
5
B
1
B
2
B
3
B
5
C
2
C
3
I have data like this,
App_Num Days Price
A1 10 100
A1 11 150
A2 11 200
A3 12 250
A3 12 300
A4 20 350
A4 21 400
The average of the days is displayed on a card visual as 13.857.
Now, there are two parameters that are set for user to adjust the values and see.
Total Value (Min and Max Range)
Days
For example, if the user selects 0-280- it is expected to list A1 (100 + 150 = 250 less than 280) and A2 (200 being less than 280).
I used a DAX like this and built a table like this,
Apps_in_scope =
Var min_amount = Min('Total Value'[Total Value])
Var max_amount = Max('Total Value'[Total Value])
var required_app_num = SELECTEDVALUE(Table1[App_Num])
Var required_amount = CALCULATE(sum(Table1[Price]),FILTER(Table1,Table1[App_Num] = required_app_num))
var in_scope = if(And(required_amount <= max_amount, required_amount >= min_amount),1,0)
return in_scope
And I was able to produce a Visual like this,
App_Num Apps_in_scope
A1 1
A2 1
A3 0
A4 0
Now after selecting the total price range, if the user selects the days parameter manually to be 15 then my average will shift as per this logic.
A1 has 2 transactions and with in the selected price range of 280 will become (15*2)
A2 has 1 transaction and with in the selected price range of 280 become (15*1)
A3 has 2 transaction and will remain unchanged (12+12)
A4 has 2 transactions and will remain unchanged (20+21)
So my new measure which I want to place on the card is expected to show now (15+15+15+12+12+20+21)/7 = 15.714
How can I write this measure. Kindly help me with this
I'd tweak your measure slightly so that it works better for taking the average:
Apps_in_scope_2 =
VAR min_amount = MIN ( 'Total Value'[Total Value] )
VAR max_amount = MAX ( 'Total Value'[Total Value] )
VAR required_amount =
CALCULATE ( SUM ( Table1[Price] ), ALLEXCEPT ( Table1, Table1[App_Num] ) )
VAR in_scope =
IF ( AND ( required_amount <= max_amount, required_amount >= min_amount ), 1, 0 )
RETURN
in_scope
With this tweak the average is fairly simple:
AvgMeasure =
VAR DaysParam = SELECTEDVALUE ( DaysSlicer[Days] )
RETURN
AVERAGEX( Table1, IF( [Apps_in_scope_2] = 1, DaysParam, Table1[Days] ) )
Edit:
Here's an alternative version that doesn't use the first measure but should scale better to large data tables.
AvgAlternate =
VAR min_amount = MIN ( 'Total Value'[Total Value] )
VAR max_amount = MAX ( 'Total Value'[Total Value] )
VAR DaysParam = SELECTEDVALUE ( DaysSlicer[Days] )
VAR apps =
ADDCOLUMNS (
SUMMARIZE (
Table1,
Table1[App_Num],
"#Price", SUM ( Table1[Price] ),
"#Rows", COUNT ( Table1[Price] )
),
"#Days",
IF (
AND ( [#Price] <= max_amount, [#Price] >= min_amount ),
DaysParam * [#Rows],
CALCULATE ( SUM ( Table1[Days] ) )
)
)
RETURN
DIVIDE ( SUMX ( apps, [#Days] ), SUMX ( apps, [#Rows] ) )
This is assuming that you have separate tables for your Price range and Days selection (as in what-if parameter tables).
My measure =
VAR apps =
SELECTCOLUMNS (
FILTER (
SUMMARIZE ( Table1, Table1[App_Num], "Total Price", SUM ( Table1[Price] ) ),
[Total Price] >= MIN ( 'Total Value'[Total Value] )
&& [Total Price] <= MAX ( 'Total Value'[Total Value] )
),
"App_Num", [App_Num]
)
RETURN
AVERAGEX (
Table1,
IF ( Table1[App_Num] IN apps, SELECTEDVALUE ( Days[Days] ), Table1[Days] )
)
I have a table like this :
Id numbers_old numbers_new date
1 5 0 2019-02-13
1 8 3 2019-02-14
2 2 0 2019-02-13
2 12 10 2019-02-14
2 15 5 2019-02-15
I want a measure which will calculate volume_total = (number_old where date=min(date)) + (numbers_new where date != min(date))
How can I achieve it with a Power BI measure?
You can write the measure:
volume_total =
VAR DateMin =
MIN ( Table1[date] )
VAR SumOld =
CALCULATE (
SUM ( Table1[numbers_old] ),
Table1[date] = DateMin
)
VAR SumNew =
CALCULATE (
SUM ( Table1[numbers_new] ),
Table1[date] <> DateMin
)
RETURN
SumOld + SumNew
Worked example PBIX file: https://pwrbi.com/so_54707672/