The workfile is available here
I wish to conduct time-based analysis on my datas.
For this purpose, I have created 2 calculation groups.
Year comparison that enables me to deal with relative yearly data :
N :=
VAR lv_MAX_YEAR =
[RELATIVE_MAX_YEAR] - 0
RETURN
CALCULATE (
SELECTEDMEASURE() ,
dim_CALENDAR[Year] = lv_MAX_YEAR
)
N-1 :=
VAR lv_MAX_YEAR =
[RELATIVE_MAX_YEAR] - 1
RETURN
CALCULATE (
SELECTEDMEASURE() ,
dim_CALENDAR[Year] = lv_MAX_YEAR
)
N/N-1 Evolution :=
VAR N = CALCULATE( SELECTEDMEASURE() , 'Year comparison'[Year comparison] = "N" )
VAR N_1 = CALCULATE( SELECTEDMEASURE() , 'Year comparison'[Year comparison] = "N-1" )
RETURN
N - N_1
Works fine for me with the expeced behaviour : N is by default the current year (max year in my calendar table), but if I filter on a given year, N becomes that one.
and Relative Period allows me to compare Sales by the number of passed day from the beginning of the year :
[MAX_YEAR_DAY] :=
VAR lv_MAX_YEAR =
CALCULATE (
MAX ( dim_CALENDAR[Year] ) ,
ALL ( dim_CALENDAR[Year] )
)
RETURN
CALCULATE (
MAX ( dim_CALENDAR[Year_day] ) ,
dim_CALENDAR[Year] = lv_MAX_YEAR
)
Year To Max Year_day :=
VAR lv_MAX_YEAR_DAY =
[MAX_YEAR_DAY]
RETURN
CALCULATE (
SELECTEDMEASURE () ,
dim_CALENDAR[YEAR_DAY] <= lv_MAX_YEAR_DAY
)
So far it's a partial success :
my grand totals seem correct ;
but my monthly details don't ;
and my yearly comparison gets busted when it's activated.
How can I use both calculation groups simultaneously?
Related
This is a simple request.
I have to create a column in query editor, or in table view. Whichever is easy.
Column looks like this -->
A,B,C,D,D,E,D
B,C,D,B,D,A
C,C,D,F,E,G
D,D,E,E,E,F,B
Result should be based on count of characters present, with 'A' character always taking the priority.
For instance result of the above column next to it will be
A ( A will take priority even if D has most count)
A (Even though B has most count, A will take Priority)
C ( as C has most count)
E ( as E has most count)
Result =
VAR String = COALESCE ( 'Table'[Column], "XX" )
VAR Items = SUBSTITUTE ( String, ",", "|" )
VAR Length = PATHLENGTH ( Items )
VAR T1 = GENERATESERIES ( 1, Length, 1 )
VAR T2 = ADDCOLUMNS ( T1, "#Item", PATHITEM ( Items, [Value] ) )
VAR T3 = GROUPBY ( T2, [#Item], "#Count", COUNTX ( CURRENTGROUP(), 1 ) )
VAR T4 = TOPN ( 1, T3, [#Count] )
VAR Result = MAXX ( T4, [#Item] )
RETURN
IF ( PATHCONTAINS ( Items, "A" ), "A", Result )
Blockquote
I'm trying to optimize a measure, and after analyzing it I found that the problem comes from the fact that part of it is calculated on every row when it only needs to be calculated once when a certain filter is applied.
Here's the measure :
Effectif :=
VAR LastPeriod =
MAX ( 'Time'[Period] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Sales'[ClientID] ),
FILTER (
Sales,
OR (
LastPeriod - Sales[ClientLastOrder] < 4,
LastPeriod - Sales[ClientEntry] < 4
)
)
)
In this case, LastPeriod will be calculated over and over, whereas we only need it to be calculated once when a filter is applied on Time.
Is there any way to store this information somewhere so that it doesn't have to make superfluous calculations ?
Try this 2 Codes, and let me know If one or both of them are any faster. Instead of iterating (or scanning ) a full table, you only iterate the columns you need in a filter argument. Like this:
Version-1
Effectif :=
VAR LastPeriod =
MAX ( 'Time'[Period] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Sales'[ClientID] ),
FILTER (
ALL ( Sales[ClientLastOrder], Sales[ClientEntry] ),
OR (
LastPeriod - Sales[ClientLastOrder] < 4,
LastPeriod - Sales[ClientEntry] < 4
)
)
)
Version-2
Effectif :=
VAR LastPeriod =
MAX ( 'Time'[Period] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Sales'[ClientID] ),
CALCULATETABLE (
SUMMARIZE ( Sales, Sales[ClientLastOrder], Sales[ClientEntry] ),
OR (
LastPeriod - Sales[ClientLastOrder] < 4,
LastPeriod - Sales[ClientEntry] < 4
)
)
)
It is hard to be confident without any data sample, but I guess that this formula of LastPeriod calculating may help you:
VAR LastPeriod =
CALCULATE(
MAX ( 'Time'[Period] ),
ALL('Sales')
)
Also you may check such function as ALLSELECTED(). It may fit you more.
I need to separate a number with commas
for example number 142531 to show as 14:25:31
Googled for a while and found something like this:
Column =
VAR right =
RIGHT ( [Column1], 3 )
VAR left =
SUBSTITUTE ( [Column1], right, "" )
RETURN
COMBINEVALUES ( ":", left, right )
This kinda shows me how to do it, I can separate the number in the middle with 1 comma but not sure how to do this with the middle part.
Time =
VAR left = LEFT ( [RCP_TIME], 2 )
VAR mid = MID ( [RCP_TIME], 3, 2 )
VAR right = RIGHT ( [RCP_TIME], 2 )
RETURN
COMBINEVALUES(":",left, mid, right)
I have two tables that contain the number of users of two types (see picture).
I have created a measure that computes the cumulative sum of the number of Users2
TotalUsers2 =
CALCULATE (
SUM ( Users_2[Users_2] ),
FILTER (
ALLSELECTED ( Users_2 ),
'Users_2'[Year_month] <= MAX ( 'Users_2'[Year_month] )
)
)
I would like to divide each row of table 1 with the rows of TotalUsers2.
For example, for 2019_01, I have 10 Users1 and 1000 in the totalUsers2, I want to obtain the 10/1000 * 100 value.
For 2019_02, 20/1500 * 100, for 2019_03, 30/1700 * 100, and so on.
Just put that measure in the denominators, except you'll need to use Users_1[Year_month] instead of Users_2[Year_month] for the MAX.
TotalUsers2 =
DIVIDE (
SUM ( Users_1[Users_1] ),
CALCULATE (
SUM ( Users_2[Users_2] ),
FILTER (
ALLSELECTED ( Users_2 ),
Users_2[Year_month] <= MAX ( Users_1[Year_month] )
)
)
)
I have a basic table that looks like this:
DayNo. Customer AgentsInvolved CallID
0 AAA 1 1858
0 AAA 3 1859
2 AAA 1 1860
0 BBB 2 1862
0 CCC 1 1863
0 DDD 3 1864
9 DDD 1 1865
9 DDD 4 1866
I need to be able to find the % of customers who only contacted only once, and spoke to 1 agent only. So from the above example, out of 4 distinct customers only customer CCC falls into this category (1 call, 1 AgentInvolved)
So the Desired result would be: 1/4 or 25%
How can I create a Power BI measure to do this calc?
Try this measure:
Desired Result =
VAR summarizetable =
SUMMARIZECOLUMNS (
'table'[Customer],
"Calls", COUNT ( 'table'[CallID] ),
"Agents", SUM ( 'table'[AgentsInvolved] ),
"Day", SUM ( 'table'[DayNo.] )
)
RETURN
COUNTROWS (
FILTER ( summarizetable, [Calls] = 1 && [Agents] = 1 && [Day] = 0 )
)
/ COUNTROWS ( summarizetable )
The summarized table created on the fly in VAR summarizetable looks like this:
Here's another approach:
Measure =
SUMX(
VALUES(Table2[Customer]),
CALCULATE(
IF(
DISTINCTCOUNT(Table2[CallID]) = 1 &&
SUM(Table2[AgentsInvolved]) = 1,
1,
0
),
Table2[DayNo.] = 0
)
) /
DISTINCTCOUNT(Table2[Customer])
If you want to exclude the 0 day rows in the denominator as well, replace the last line with
CALCULATE(DISTINCTCOUNT(Table2[Customer]), Table2[DayNo.] = 0)
Desired Result =
VAR summarizetable =
SUMMARIZECOLUMNS (
'table'[AgentsInvolved],
'table'[DayNo.],
'table'[Customer],
"Calls", COUNT ( 'table'[CallID] )
)
)
RETURN
COUNTROWS (
FILTER ( summarizetable, [Calls] = 1 && 'table'[AgentsInvolved] = 1 && 'table'[DayNo.] = 0 )
) / DISTINCTCOUNT ( 'table'[Customer])