Conditional Column in PowerBI based on Priority and Count - logic

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

Related

Not able to access the INIT variable in a secondary FOR loop

I have the following code snippet:
TYPES: BEGIN OF ty_table_line,
idx TYPE i,
posnr TYPE i,
quan TYPE i,
END OF ty_table_line,
ty_internal_table TYPE SORTED TABLE OF ty_table_line WITH UNIQUE KEY idx.
DATA(lt_set) = VALUE ty_internal_table( ( idx = 1 posnr = 1 quan = 2 )
( idx = 2 posnr = 1 quan = 40 )
( idx = 3 posnr = 1 quan = 10 )
( idx = 4 posnr = 1 quan = 88 ) ).
DATA(lt_set_2) = VALUE ty_internal_table(
FOR i = 0 WHILE i <= 4
( LINES OF VALUE #( FOR <line> IN lt_set[]
WHERE ( idx = i + 1 )
( <line> ) ) ) ).
Here, my loop starts with i=0 and I am trying to use the value in i to filter out some values from an internal table LT_SET. What is the right approach for this error?
I tried with WHERE ( idx = i + 1 ) and WHERE ( idx <> i ), but I get this error:
The variable "I" cannot be used here.
I can't explain the reason (nothing found in the ABAP documentation) but I could reproduce and find a workaround with an additional auxiliary variable declared with LET. Tested with ABAP 7.52.
TYPES: BEGIN OF ty_table_line,
idx TYPE i,
posnr TYPE i,
quan TYPE i,
END OF ty_table_line,
ty_internal_table TYPE SORTED TABLE OF ty_table_line WITH UNIQUE KEY idx.
DATA(lt_set) = VALUE ty_internal_table( ( idx = 1 posnr = 1 quan = 2 )
( idx = 2 posnr = 1 quan = 40 )
( idx = 3 posnr = 1 quan = 10 )
( idx = 4 posnr = 1 quan = 88 ) ).
DATA(lt_set_2) = VALUE ty_internal_table(
FOR i = 0 WHILE i <= 4
LET j = i IN
( LINES OF VALUE #( FOR <line> IN lt_set[]
WHERE ( idx = j + 1 )
( <line> ) ) ) ).
ASSERT lt_set_2 = lt_set.

Time based Calculation groups - apply year to date to a yearly comparison

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?

Using SELECTEDVALUE with PowerPivot instead of Power BI

I moved a report from Power BI to Power Pivot, but the formula of SELECTEDVALUE does not exist in Power Pivot.
Can you help correct to a function that works in Power Pivot?
las_val =
VAR y =
SELECTEDVALUE ( DateTime[Gasday] )
RETURN
CALCULATE (
HEML_TTF_PRICE_V[TTF_DA_WE],
TOPN (
1,
FILTER (
ALLSELECTED ( DateTime[Gasday] ),
DateTime[Gasday] <= y
&& NOT ( ISBLANK ( HEML_TTF_PRICE_V[TTF_DA_WE] ) )
),
DateTime[Gasday], DESC
)
)
SELECTEDVALUE is a newer function that's a shortcut for
IF ( HASONEVALUE ( DateTime[GasDay] ), VALUES ( DateTime[GasDay] ) )

DAX to separate number with commas

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)

how to filter out null value then compare in oracle

I tried this, and it works,
SELECT name
FROM
(SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL
)
WHERE LENGTH >= ALL
(SELECT LENGTH FROM
(SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL
)
)
but my final code would be like this:
SELECT a.name,
a.length
FROM
(SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL
) a,
geo_river b,
encompasses c
WHERE a.length >= ALL
(SELECT a2.LENGTH
FROM
(SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL
) a2
)
AND a.name = b.river
AND b.country = c.country
AND c.continent = 'America'
this is really complicated.
Is there an easy way to let
(SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL)
be river, so I don't need to use this
(SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL)
two times.
If you want to simplify your code writing, you can use WITH:
with viewA as (SELECT name,LENGTH FROM river WHERE LENGTH IS NOT NULL )
SELECT a.name,
a.length
FROM
viewA a,
geo_river b,
encompasses c
WHERE a.length >= ALL
(SELECT a2.LENGTH
FROM
viewA a2
)
AND a.name = b.river
AND b.country = c.country
AND c.continent = 'America'
Using a single table scan:
SELECT name,
length
FROM (
SELECT name,
length,
RANK() OVER ( ORDER BY length DESC ) AS rnk
FROM river
)
WHERE rnk = 1;
So your code would then be:
SELECT a.name,
a.length
FROM (
SELECT name,
length
FROM (
SELECT name,
length,
RANK() OVER ( ORDER BY length DESC ) AS rnk
FROM river
)
WHERE rnk = 1
) a
INNER JOIN
geo_river b
ON ( a.name = b.river )
INNER JOIN
encompasses c
ON ( b.country = c.country )
WHERE c.continent = 'America';
I don't think you need to filter out null lengths as they won't show up if you subset on length (i.e. when comparing NULL values without using the NVL function the comparison will always evaluate to false and not show the row). So something simple such as:
Select a.name, a.length
from river a, geo_river b, encompasses c
WHERE a.length > 0
AND a.name = b.river
AND b.country = c.country
AND c.continent = 'America'
;
Will do the trick

Resources