Text.StartsWith but with a number field? - powerbi

I've got the below as a transformation step against a table, but it isn't working. I think it's due to the fact that it's not a text field, but a number field.
= Table.ReplaceValue(
#"Custom1",
each [Sign],
each if Text.StartsWith([Account No], "4") then -1 else [Sign] ,
Replacer.ReplaceText,{"Sign"}
)
If I create a Step with the following, I get correct results for those [Account No] valuese that start with a 4.
= Table.AddColumn(#"Replaced Value2", "AccountNoStartsWithFour", each Text.StartsWith([Account No], "4"))
Does anyone know what I need to do to get this working?
There are values such as:
4001
4201
1240
3556
I have a sample file here: https://easyupload.io/7v68iw

Is this what you require?
= Table.ReplaceValue(AccountNoStartsWithFour, each [Sign] ,each if Text.StartsWith([Account No], "4") then -1 else [Sign],Replacer.ReplaceValue,{"Sign"})

Related

Calculate the difference between 2 rows in PowerBI using DAX

I'm trying to complete something which should be quite simple but for the life of me, I can't work it out.
I'm trying to calculate the difference between 2 rows that share the same 'Scan type'.
I have attached a photo showing sample data from production. We run a scan and depending on the results of the scan, it's assigned a color.
I want to find the difference in Scan IDs between each Red scan.
Using the attached Photo of Sample data, I would expect a difference of 0 for id 3. A difference of 1 for id 4 and a difference of 10 for id 14.
I have (poorly) written something that works based on the maximum value from the scan id.
I have also tried following a few posts to see if I can get it to work..
var _curid= MAX(table1[scanid])
var _curclueid = MAX(table1[scanid])
var _calc =CALCULATE(SUM(TABLE1[scanid],FILTER(ALLSELECTED(table1[scanid]),table1[scanid]))
return if(_curid-_calc=curid,0,_curid-_calc)
Edit;
Forgot to mention I have checked threads;
57699052
61464745
56703516
57710425
Try the following DAX and if it helps then accept it as the answer.
Create a calculated column that returns the ID where the colour is Red as follows:
Column = IF('Table'[Colour] = "Red", 'Table'[ID])
Create another column as following:
Column 2 =
VAR Colr = 'Table'[Colour]
VAR SCAN = 'Table'[Scan ID]
VAR Prev_ID =
CALCULATE(MAX('Table'[Column 2]),
FILTER('Table', 'Table'[Colour] = Colr && 'Table'[Scan ID] < SCAN))
RETURN
'Table'[Column] - Prev_ID
Output:
EDIT:-
If you want your first value(ID3) to be 0 then relace the RETURN line with the following line:
IF(ISBLANK(Prev_ID) && 'Table'[Colour] = "Red", 0, 'Table'[Column] - Prev_ID )
This will give you the following result:

Power BI - "Other" Classfication Based on Percentage of Total

quick question:
I have a total amount that is divided into several classifications, like so:
Total: 7bn
Classification 1: 3bn,
Classification 2: 1bn,
... ,
Classification N: 0,3M
N is such a big number that when I put in a graph, most of the classifications don't even show up in there, so my manager suggested that I took anything that represents less than 5% of the total 7bn and classified them as "Others" to put it all together in the visual.
Then I made a measure "% of total" like:
% of total =
divide(
sum(values),
sumx(
allselected(table),
values
)
)
And this actually works perfect, except...
I wanna make a measure (or calculated column) that returns something like:
new classification =
if(
[% of total] > 0.05,
"Others",
[classification]
)
just to classify for me in the graph
but then only one of the new classifications returns as the old one, the rest returns "Others", but I know there's more than one, according to [% of total].
Can you think of another way to make this work? Is this a dumb question?
Thanks in advance
Create 2 separate measure for [others] & [classification] and create your final measure as below-
new classification =
var is_greater = IF([% of total] > 0.05, 1, 0)
RETURN
SWITCH(
is_greater ,
1,[Others]",
[classification]
)

DAX function that check if value is in range and return corresponding value in other table

I have two tables: a "Range" table that has the ranges and its corresponding values and a "Data" table that has the values I want to check against the ranges.
My Data table is as follows, but it has more rows than the Range table. The "PM" values are the ones that I need to check.
Location PM
B01 1,05
B02 1,04888
B15 1,05787
B16 1,05787
B03 2,03714
B04 2,03714
B09 2,03714
B10 2,03714
B17 2,03714
And the "Range" table is like this sample:
P.E P.S PV
0,48 1,03 10,00%
1,03 2,02 10,03%
2,02 3,63 8,87%
3,63 6,23 8,24%
6,23 10,17 7,62%
10,17 15,79 6,46%
15,79 22,37 5,75%
22,37 30,70 5,29%
30,70 41,27 4,99%
41,27 54,88 4,86%
54,88 71,57 4,65%
So, summing up, I need to create a DAX measure or column that checks if the PM value is between the PE and PS values and return the corresponding PV.
On excel, I managed to do this usind the LOOKUP function, as this function rounds the searched value to the nearest smaller value in the corresponding table to give a match. On Power Bi I coudn't find a way to replicate this.
Does someone knows if it's possible?
Thanks for all the help!
You need a measure as below-
respected_pv =
CALCULATE(
MAX(range[PV]),
FILTER(
ALL(range),
range[P.E] <= MIN(data[PM])
&& range[P.S] >= MIN(data[PM])
)
)
Here is the code for a Custom Column-
respected_pv_column =
CALCULATE(
MAX(range[PV]),
FILTER(
ALL(range),
range[P.E] <= data[PM]
&& range[P.S] >= data[PM]
)
)
Here below is the sample output. Remember, only first row get the PV as other ranges are not available in your sample data.

How to to solve divide by zero error (NaN - Not a Number) in my formula in Power BI?

I have a column with a formula which reads:
Utilisation (Excl. Time-off) = Utilisation_Excl_Timeoff[Billable Hours]/(Utilisation_Excl_Timeoff[Available Hours] - Utilisation_Excl_Timeoff[Timeoff Hours (Excl. Public)])
It gives me a "NaN" error in my calculated column for some of the cells due to divide by zero.
I would like to replace the NaN with a 0% instead so that the column displays correctly in my matrix chart.
Take a look at the DIVIDE function (https://msdn.microsoft.com/en-us/library/jj677276.aspx).
This is a 'safe divide` function with the option to return an alternative value if the division returns an error.
-JP
If you are looking for solution in M, then add conditional column:
Set up Otherwise temporarily to dividend column, here I took [Value] column. After doing this change in editor [Value] to [Value]/[Units]. This returns null wherever Units is 0. You may change returned output to 0% according to thy wish.
Alternatively, you can do it as well by adding this step:
= Table.AddColumn(#"Previous Step", "UnitPrice", each if [Units] = 0 then "0%" else [Value]/[Units])
IFERROR(value, value_if_error) function can do this. MSDN
Utilisation (Excl. Time-off) = IFERROR(Utilisation_Excl_Timeoff[Billable Hours]/(Utilisation_Excl_Timeoff[Available Hours] - Utilisation_Excl_Timeoff[Timeoff Hours (Excl. Public)]), 0)

multiple if's for calculated field in tableau

please pardon the absolutely newbie question but i'm very new to tableau.
what I'd like to do is create a message based on which filter flags are active. so, in psuedo code, i'd do something like this:
message = ''
if filter1 == 1:
message += 'filter 1 is active'
if filter2 == 1:
message += ' filter 2 is active'
return message
problem is, I'm not even sure how to do multiple if statements - i keep getting a syntax error. Any help will be greatly appreciated.
Here is an example of how I accomplished something similar:
IF [ZAVUFA1_FED_COLL_CHOICE_1] = 'xxxxx' THEN 1
ELSEIF [ZAVUFA1_FED_COLL_CHOICE_2] = 'xxxxx' THEN 2
ELSEIF [ZAVUFA1_FED_COLL_CHOICE_3] = 'xxxxx' THEN 3
ELSEIF [ZAVUFA1_FED_COLL_CHOICE_4] = 'xxxxx' THEN 4
ELSEIF [ZAVUFA1_FED_COLL_CHOICE_5] = 'xxxxxx' THEN 5
ELSEIF [ZAVUFA1_FED_COLL_CHOICE_6] = 'xxxxx' THEN 6
ELSEIF [ZAVUFA1_FED_COLL_CHOICE_7] = 'xxxxxx' THEN 7
ELSEIF [ZAVUFA1_FED_COLL_CHOICE_8] = 'xxxxxx' THEN 8
ELSEIF [ZAVUFA1_FED_COLL_CHOICE_9] = 'xxxxx' THEN 9
ELSEIF [ZAVUFA1_FED_COLL_CHOICE_10] = 'xxxxxx' THEN 10
ELSEIF ISNULL([ZAVUFA1_FED_COLL_CHOICE_1]) THEN 99
END
As much as I love stackoverflow, Tableau also has a great user forum on their site.
You would create a calculated field called message with this code:
IF filter1 = 1 THEN 'filter 1 is active' END
+ IF filter2 = 1 THEN ' filter 2 is active' END
what I ended up doing is creating a calculated field for each if statement. I then created yet another calculated field that concatenates all of the output from each of the first set of calculated fields I created. Seems like a bit of a hack so If anyone knows of a more elegant way of doing this (making a calculated field of a series of calulated fields seems awfully kludgy) I'd be glad to pass on the points for answering.