PowerBi DAX - Variable (CONCATENAX) in operator "IN" - powerbi

Exists a form to do a variable Concatenax in operator "IN" on dax?
example:
Max value =
VAR SUC = CONCANATEX(FILTERS (TABLE[COLUMN]; TABLE[COLUMN]; ";")
VAR SOLUTION = CALCULATE(SUM(TABLE[COLUMN2]; TABLE[COLUMN] IN {SUC})
RETURN SOLUTION
my code return nothing when have 2 values in SUC.
Thanks.

Related

How to mark column with 0 or 1 if data exists in another table in Power BI?

I have 2 tables
1 is a set of employees (Table 1)
1 is a set of terminations (Table 2)
They will both match on an Employee ID column. I want to add a new calculated column to Table 1 that returns 1 if the employee is in Table 2 and returns 0 otherwise. I can't figure out how to write this in DAX. I feel like this should be extremely simple.
I tried
Column =
VAR X = RELATED(Table1[Employee ID])
VAR RES = IF(ISBLANK(X), "no data", X)
RETURN
RES
This just returns "#ERROR" in all values.
Make sure your IF statement returns the same data type for both the true and the false section:
use either
VAR RES = IF(ISBLANK(X), "no data", FORMAT(X, "#"))
or
VAR RES = IF(ISBLANK(X), 0, X)
And referring to the title of your question you should actually use
VAR RES = IF(ISBLANK(X), 0, 1)

power bi DAX hierarchical table concatenation of names

Since two days I'm on a problem and I can't solve it so I come here to ask some help...
I have that bit of dax that basically take the path of a hierarchical table (integers) and take the string names of the 2 first in the path.
the names I use:
'HIERARCHY' the hierarchical table with names, id, path, nbrItems, string
mytable / addedcolumn1/2 the new table used to emulate the for loop
DisplayPath =
var __Path =PATH(ParentChild[id], ParentChild[parent_id])
var __P1 = PATHITEM(__Path,1) var __P2 = PATHITEM(__Path,2)
var l1 = LOOKUPVALUE(ParentChild[Place],ParentChild[id],VALUE(__P1))
var l2a = LOOKUPVALUE(ParentChild[Place],ParentChild[id],VALUE(__P2))
var l2 = if(ISBLANK(l2a), "", " -> " & l2a)
return CONCATENATE(l1,l2)
My problem is... I don't know the number of indexes in my path, can go from 0 to I guess 15...
I've tried some things but can't figure out a solution.
First I added a new column called nbrItems which calculate the number of items in the list of the path.
The two columns:
Then I added that bit of code that emulates a for loop depending on the number of items in the path list, and I'd like in it to
get name of parameters
concatenate them in one string that I can return and get
string =
var n = 'HIERARCHY'[nbrItems]
var mytable = GENERATESERIES(1, n)
var addedcolumn1 = ADDCOLUMNS(mytable, "nom", /* missing part: get name */)
var addedcolumn2 = ADDCOLUMNS(addedcolumn1, "string", /* missing part: concatenate previous concatenated and new name */)
var mymax = MAXX(addedcolumn2, [Value])
RETURN MAXX(FILTER(addedcolumn2, [Value] = mymax), [string])
Full table:
Thanks for your help in advance!
Ok, so after some research and a lot of try and error... I've came up to a nice and simple solution:
The original problem was that I had a hierarchical table ,but with all data in the same table.
like so
What I did was, adding a new "parent" column with this dax:
parent =
var a = 'HIERARCHY'[id_parent]
var b = CALCULATE(MIN('HIERARCHY'[libelle]), FILTER(ALL('HIERARCHY'), 'HIERARCHY'[id_h] = a))
RETURN b
This gets the parent name from the id_parent (ref. screen).
then I could just use the path function, not on the id's but on the names... like so:
path = PATH('HIERARCHY'[libelle], 'HIERARCHY'[parent])
It made the problem easy because I didn't need to replace the id's by there names after this...
and finally to make it look nice, I used some substitution to remove the pipes:
formated_path = SUBSTITUTE('HIERARCHY'[path], "|", " -> ")
final result

LOOP to Look up the value in M Query

I have a following data table:
I need to create a column called FINAL VALUE with certain rules in the power query editor:
The FINAL VALUE is created based on the COUNTRY
If VALUE is greater than 100, then the previous value will be taken
if the previous values (no previous value such as 202001) are all greater than 100, then 100
So the final table should look like:
I sort COUNTRY and DATE, and then I try to apply loop in M Query. As I am not familiar with the function in M query, I have hard time figuring out. I appreciate any help. Thank you.
I believe you are better of in DAX. You can create a new column like below:
Final Value =
var curCountry = TableCountry[Country]
var curDate = TableCountry[Date]
var prevDate = CALCULATE(MAX(TableCountry[Date]), FILTER(TableCountry, curDate >= TableCountry[Date] && curCountry = TableCountry[Country] && TableCountry[Value] <=100))
return LOOKUPVALUE(TableCountry[Value], TableCountry[Country], curCountry, TableCountry[Date], prevDate, 100)

Power bi group ranking

Can you help me? I want the rank to be assigned within the filtered range (filter by date). Tell me, please, what am I doing wrong? CustomerSaleIndex1 = VAR CurrentOrderDate = \'public _smsnt_delete\'[date_transaction] VAR CurrentOrderTr = \'public _smsnt_delete\'[transaction_id] VAR CurrentCustomerKey = \'public _smsnt_delete\'[card_number] RETURN RANKX(FILTER(ALLSELECTED(\'public _smsnt_delete\'); \'public _smsnt_delete\'[card_number] = CurrentCustomerKey);\'public _smsnt_delete\'[date_transaction];;ASC;Dense)
In terms of your separator in the filter function, you are using the semicolon ";" over the comma separator ","?
Also you've declared CurrentOrderDate as a variable, why not use it. On the other side of things, why is CurrentOrderTr even declared? As you don't use it in the function:
CustomerSaleIndex1 =
VAR CurrentOrderDate = 'public _smsnt_delete\'[date_transaction]
VAR CurrentCustomerKey = 'public _smsnt_delete\'[card_number]
RETURN
RANKX(FILTER(ALLSELECTED('public
_smsnt_delete\'),'public_smsnt_delete\'[card_number] = CurrentCustomerKey),
CurrentOrderDate,,ASC,Dense)
https://learn.microsoft.com/en-us/dax/rankx-function-dax

PowerBi subtracting two cells in different rows with condition

I am wondering if something that i would like to achieve is possible, please look at the picture and read description below:
I would like to add a column to the right, where if a cell table[ActionType] = "TERMINATING", it calculates a difference between timestamps (timestamp for TERMINATING - timestamp for STARTING in below row). If the result is positive (>0) then store it in a column in a corresponding row (eg next to timestapm for terminating), if result is negative don't store it. And all of that applied to whole table.
I tried conditional column and i guess it cannot be done with this or at least I couldn't make it.
Will be very thankful for responses and tips!
Pre-requisite :- Add an Index Column using the query editor. Make sure they are in the next row to each other.
It is advisable to keep TimeStamp column as a DateTime Column itself.
So, if you can change your TimeStamp column to a DateTime column then try this :-
Difference =
Var Get_Action_Type = Table1[ActionType]
Var required_Index_1 = Table1[Index] + 1
Var required_Index = IF(required_Index_1 > MAX(Table1[Index]),required_Index_1-1, required_Index_1)
Var Current_Action_TimeStamp = Table1[TimeStamp]
Var next_Action_TimeStamp = CALCULATE(MAX(Table1[TimeStamp]),FILTER(Table1, Table1[Index] = required_Index))
Var pre_result = IF(Get_Action_Type = "TERMINATING", DATEDIFF(Current_Action_TimeStamp, next_Action_TimeStamp,SECOND), BLANK())
Var result = IF(pre_result > 0, pre_result, BLANK())
return result
And if you cannot change it to a Date Time, then try this calculated column,
Difference_2 =
Var Get_Action_Type = Table1[ActionType]
Var required_Index_1 = Table1[Index] + 1
Var required_Index = IF(required_Index_1 > MAX(Table1[Index]),required_Index_1-1, required_Index_1)
Var Current_Action_TimeStamp = Table1[Time_Stamp_Number]
Var next_Action_TimeStamp = CALCULATE(MAX(Table1[Time_Stamp_Number]),FILTER(Table1, Table1[Index] = required_Index))
Var pre_result = IF(Get_Action_Type = "TERMINATING", next_Action_TimeStamp - Current_Action_TimeStamp, BLANK())
Var result = IF(pre_result > 0, pre_result, BLANK())
return result
The Output looks as below :-
Kindly accept the answer if it helps and do let me know, how it works for you.