Power Query M: Count in how many rows a certain substring occours - powerquery

I have this type of table in Power BI:
id
isPromoter
path
aaa-111-000
false
sqe-432-w14/2aq-4ec-t66/aaa-111-000/
sss-342-r34
true
a3e-543-1sd/34d-245-svt/s3a-bnj-klo/sss-342-r34/
hhy-e90-y7u
false
a3e-543-1sd/34d-245-svt/s3a-bnj-klo/sss-342-r34/hhy-e90-y7u/
...
...
...
So, as you can see, the second id is contained in both in the second and in the third path; this only can happen if the user is a Promoter.
I would like another field which counts how many times each id is contained in all paths (beside itself); so it should be 0 if the user is not a promoter and >0 if it is.
id
isPromoter
path
children
aaa-111-000
false
sqe-432-w14/2aq-4ec-t66/aaa-111-000/
0
sss-342-r34
true
a3e-543-1sd/34d-245-svt/s3a-bnj-klo/sss-342-r34/
3
hhy-e90-y7u
false
a3e-543-1sd/34d-245-svt/s3a-bnj-klo/sss-342-r34/hhy-e90-y7u/
0
...
...
...
...
I know that there is the function Text.Contains([path],[id]) but it is only true for the current row. I don't know how to do the count for all rows

Add column ... custom column ... name it children, use formula
= List.Count(List.FindText(#"PriorStepNameHere"[path],[id]))-1

Related

How to slice the dataset in Python in specific intervals

I have a dataset with n rows, how can I access a specific number of rows every specific number of rows through the whole dataset using Python?
For example, in 100 rows data set and I want to access 10 rows every 10 rows, like 1:10, 20:30, 40:50, 60:70, 80:90
I could think of something like this
df.iloc[np.array([int(x/10) for x in df.index]) % 2 == 0]
It takes the index of the dataframe, divides it by 10 and casts it to an int. This basically just removes the last digit in this example.
With the modulo statement the first 10 rows are True, the next 10 False and so on. This is then used with iloc to get just the lines with the True value.
This requires a continuously increasing index. If for example some rows were already filtered out this is not the case. reset_index can be used to reset the index.

Overwrite values in one column with values from another unless null PowerQuery

Index
Is MTO
ShouldBeMTO
1
TRUE
null
2
TRUE
null
3
TRUE
null
4
TRUE
null
5
FALSE
null
6
TRUE
TRUE
7
FALSE
TRUE
8
etc.
...
I have data like that; I want to overwrite Is MTO with values from ShouldBeMTO except when null. - e.g. lines 1 to 5 stay the same, line 7 goes from False to True
The Merge columns option concatenates columns with a delimiter. I was thinking of trying to replace null with something then XOR the columns but that's not quite what I want.
Any hints?
Oh it's super simple:
Add Column->Add Custom Column then
= if [ShouldBeMTO] = null then [Is MTO] else [ShouldBeMTO]
I was worried the syntax would look at entire columns of data but this is row-by row

How to filter a column according to values of another column in Tableau

Suppose that my query is 'A' for the following table. I want to find any value of 'c_index' corresponding to 'A', and then get all the rows of the table which have the corresponding values of 'c_index'.
Node Name
c_index
A
1
B
1
A
2
C
2
B
3
D
3
C
4
E
4
Values of 'c_index' corresponding to 'A' are {1, 2}. So the desired result of the filter is:
Node Name
c_index
A
1
B
1
A
2
C
2
How can I do this filtration in Tableau?
What I tried is:
Defined a filter on 'c_index' (i.e. drag and drop 'c_index' to the filter shelf). And then I tried to define the condition for the filter as: [Node Name] = 'A'.
But it throws an error: "The formula must be an aggregate calculation or refer only to this field".
First Join the (data) table with itself on the column which you want to return linked values. In the example c_index.
Now there will two same data sets in your data pane.
Add node from first dataset to filter, node from second dataset to view and c_index from anyone to view. You'll get what you desire. See GIF below

spotfire increment when finding a 'true' value

I want to create a calculated column that increments by 1 every time it meets a true in bool column. any ideas ?
in the image at left is the data table and at right the desired result
Something like the formula below should work
case when [bool]=True then sum(If([bool]=True,1,0)) over (AllPrevious([id])) else 0 end

Adding different row values for Birt

I got this selection of data from my sql:
I would like to add Cancelled, Disputed and Resolved together and then divide the result with the total shipped. All of this should be done with an Expression.
So x / 303 where x is the sum of the desired values.
Goal would be to get a % where I can tell how good my shipping is.
I would then like to display the result in a text label next to a graph.
How do I do that?
You should use computed columns in your data set:
Add a SUM on the column Total and a filter only matching the rows based on the column Status you want to select. The expression should look like:
if (row["Status"] == "Cancelled" || row["Status"] == "Disputed"
|| row["Status"] == "Resolved")
true
else
false
create a second computed column only containing the "Total" value where the Status is Shipped.
if (row["Status"] == "Shipped")
row["Total"]
Then create a third computed column where you divide both computed values and you are done.
row["sum"] / row["shipped"]
create a new parameter and refer the image
create new static values and allow multiple values to be selected.
So, accordingly edit your SQL queries

Resources