Countifs in google sheet - if-statement

I have problem with using COUNTIF in a formula,
I have three columns: O with values Yes, AE with values returning self-serve, self-serve and assisted and AH with dates.
I need to count based on each date users that have yes in column O and having only returning self-serve and self-serve in column AE (exclude those with value assisted). I tried to this formula:
=IF(OR(AND('New Self Serve'!AE:AE="new self-serve",'New Self Serve'!O:O="yes"),AND('New Self Serve'!AE:AE="returning self-serve",'New Self Serve'!O:O="yes")),countifs('New Self Serve'!AH:AH,A597))
but it doesn't work properly and returns values FALSE.

You mentioned:
I need to count based on each date users that have yes in column O and having only returning self-serve and self-serve in column AE (exclude those with value assisted).
You could use a query instead
=QUERY(O2:AH17,"select count(O) where AE contains 'self-serve' and O='yes' group by AH label count(O) 'COUNTED'")
Functions used:
QUERY

Related

Categorised data in PowerBi

I am looking for a suggestion on how to categorised/group data in PowerBi.
For example,
I have set up a conditional column in Power Query to achieve the results seeing in “Group” column by saying if ID is 8304 then Group B, if ID is 8660 then Group F -- but the database is large and I am already facing a performance issue when trying to set up a report based on individual Groups, it takes long to load the data.
Is there any alternative or better approach to group data?
ID
Group
8015
A
8020
A
8229
A
8304
B
8389
B
8391
C
8414
D
8421
A
8469
A
8572
A
8619
F
8660
F
8663
J
9102
A
9104
K
9120
A
I have set up a conditional column in Power Query to achieve the results seeing in “Group” column by saying if ID is 8304 then Group B, if ID is 8660 then Group F
Instead of a conditional column, use a helper table to store these links.
You can add the information to your main table by joining the two tables.

Filtering by keyword (string) in PowerBI

I have a data visualization in PowerBI that is powered by SQL on the back-end. I have one particular field that I'd like to be able to filter by string-matching. Here's what the field looks like:
me_drug_occurence
AB
C
ABC
BC
B
Each letter is unique and stands for a type of drug. More letters = combinations of drugs. No letter repeats more than once for a record. I want to use the "Filters on the page" option and have the user be able to filter by drug A, B, and C. By selecting A, for example, that would show any record that contains A (records AB, ABC,). Selecting A and B would show any record which contains A OR B (records AB, ABC, BC, B). And so on and so forth.
My issue is that there doesn't seem to be an out-of-the-box way to do this in Power BI? If I simply drag this column to the "filters on this page" sidebar, I just get options to filter by the different drug combinations.
If I choose "advanced filter", I can get closer to my goal, but it forces the user to put the keywords in manually:
So, my question is how can I accomplish a filter on this visualization that would look something like this:
Filter:
A
B
C
Where you could filter by any record that contained A, B, or C, or some combination thereof. Do I need to create a custom measure?
You already are on right path.
Use Advanced filtering.
In this use OR condition
I.e
drug contains C or drug contains B or drug contains A
This will filter out your records with only those which have either of above 3 condition matched.
Edit:
This is closest I could come up with your req.
1 Use slicer, for this slicer add filed drug occurence make it dropdown and searchable.
Now as a end user I will type in A so i get N no of records which will contain A.
As I user I will select all those records as screenshot below
and then If I type C it will show as below and select them as well
This will be the result
Now there is one filter on market place called Text Filter. Import it from marketpalce and there you can search like text (contains) but there you cannot add three different conditions. It will look something like below
Without text filter
After adding text for filter

What's really happening when this Filter(All(Column),...) syntax is evaluated?

I have only one table named 'Data' in my PowerBI Desktop model, which contains [Datetime], [Type], [Name], and several other columns.
I choose a specific Type="B" by clicking on a graph, and a period of time with a slicer on Datetime, and then use the measures below to calculate how many different names there are in Type "A"(as expected, there should be no record with A and B at the same time).
That's when I'm totally confused by the result:
(My PowerBI version is the latest release in April,2019.)
Wrong =
CALCULATE (
DISTINCTCOUNT ( Data[Name] ),
FILTER ( ALL ( Data[Type] ), Data[Type] = "A" )
)
While this measure turns out to be the right one:
Correct =
CALCULATE (
DISTINCTCOUNT ( Data[Name] ),
FILTER ( Data, Data[Type] = "A" )
)
What I think is happening (probably missing something important):
Since the two measures share the same explicit filter context outside CALCULATE, and what they finally calculate are the same, the only problem would be "what's the final context provided by FILTER"?
With the Correct version, FILTER simply takes the (Type="B" + Datetime) subset, try to find rows with Type "A" inside the subset(Type="B" + Type="A" + Datetime), and it just fails. So FILTER gives nothing to final calculation, which turns out to be blank as expected(there should be no A Type record when I choose Type B).
With the Wrong version, FILTER (with only one column) ignores all filter context on Type(originally "B"), then it applies a new one (Type="A") to replace the original one. And since each column is filtered separatedly, the filter on Datetime does not change at all. So the final context taken by CALCULATE should be a subset which contains Type "A" and the selected period of time at the same time(Type="A" + Datetime), which makes the final result "the number of distinct name of Type A during the time", having nothing to do with what Type I chose at first.
But the thing is, according to the strange result it gives, [Wrong] does absolutely not what I'm thinking of, and I have no clue on it. I've tried many ways I think reasonable to test how it works, but they just fail...
Thank you for any advice!
I made a tiny pbix file for test(with the same structure and problem):
https://pan.baidu.com/s/1gNZDNlICFLkMdPpPArb8cQ
If needed, use yf7f to download it.
The FILTER function takes a table as its first argument. This table is evaluated within the filter context you are operating in.
In the Correct version, that means when you pass in Data, that table is filtered on Type = "B" and Datetime according to your slicer selections. You then add the condition Data[Type] = "A" which is not true for anything in the filtered Data set since you already picked "B" for the type. Therefore, it returns blank since the table is empty.
Edit: Scratch what I said before and look at an example. Start with this as a full table:
Name Type Datetime
Alex A 1/3/2019
Alex A 1/4/2019
Bob A 1/5/2019
Bob B 1/5/2019
Bob A 1/7/2019
Carla B 1/3/2019
Carla B 1/4/2019
Dan A 1/6/2019
If I slice on type B and dates 1/3/2019 - 1/5/2019, here are the remaining rows:
Name Type Datetime
Bob B 1/5/2019
Carla B 1/3/2019
Carla B 1/4/2019
When we calculate ALL( Data[Type] ) within this context we get the following table, which is the same as if you remove the Type slicer but keep the date slicer:
Name Type Datetime
Alex A 1/3/2019
Alex A 1/4/2019
Bob A 1/5/2019
Bob B 1/5/2019
Carla B 1/3/2019
Carla B 1/4/2019
Now when you add the Data[Type] = "A" condition you get this table, which is the same as if you had originally filtered on A instead of B (and keep the date slicer), you get the following:
Name Type Datetime
Alex A 1/3/2019
Alex A 1/4/2019
Bob A 1/5/2019
This clearly has two distinct names instead of none. In the Correct version, the difference is that you filter for type A on the 2nd table above instead of the 3rd.
Basically, the ALL undoes the selection of type that you chose with the slicer.
Note: What I said before about indirectly affecting things isn't what's happening here. That's a concern when you are doing a context transition from row context to filter context, but doesn't apply here. Sorry for the confusion.
Finally, I've found the reason why it is happening!
It is called the overwrite of arbitrarily shaped filters.
The complete explanation can be found on "Definitive Guide to DAX", chapter 10, page 439-443.
Just be careful to use more than one filter on two or more columns of the same table, which is the easiest way that may cause the problem!

How do you escape a column name in Google visualisation API query language?

I have a Google sheet which generates an error in the following expression:
=query(Capacity!A5:FE135,"SELECT C,A WHERE "&SUBSTITUTE(ADDRESS(1,match(D2,Capacity!A1:FE1,0)+2,4),"1","")&" = '"&C2&"' AND "&SUBSTITUTE(ADDRESS(1,match(D2,Capacity!A1:FE1,0),4),"1","")&" = 1 ORDER BY C")
for a single, specific input value (a date) at D2.
Essentially, the purpose of the code is to find the column location of the date at D2 in a second sheet (Capacity) and put the values of that column in that sheet into column C in the current sheet, while also selecting only rows that match on a second column. When the date is set to a specific value, however, the expression will not evaluate.
On breaking this massive expression down into its component parts, it turns out the problem is caused by this expression:
=SUBSTITUTE(ADDRESS(1,match(D2,Capacity!A1:FE1,0)+2,4),"1","")
which, for the offending date, is returning column BY.
This means the expression being evaluated in Google Visualization API query language is:
SELECT C,A WHERE BY = '' AND BW = 1 ORDER BY C
but the query language sees BY as a reserved word, not a column, and barfs.
How can I escape the column name somehow to make it clear that it is to be considered a column name?
The way is to surround the offending portion with back-quotes (as I used to make text monospaced here):
=query(Capacity!A5:FE135,"SELECT C,A WHERE `"&SUBSTITUTE(ADDRESS(1,match(D2,Capacity!A1:FE1,0)+2,4),"1","")&"` = '"&C2&"' AND `"&SUBSTITUTE(ADDRESS(1,match(D2,Capacity!A1:FE1,0),4),"1","")&"` = 1 ORDER BY C")
so the query will look like
SELECT C,A WHERE `BY` = '' AND `BW` = 1 ORDER BY C
I assume this will help when the sheet grows so big that we're on column IF as well.

Reference & replace value in cell with value in range in google spreadsheet

I'm trying to do something which should be fairly trivial in Google Spreadsheet: I have a database of people in one sheet defined by "ID" and "Label" where the ID is a consecutive number and Label is the Name Surname of each person.
I'd need to be able, in a different sheet, to allow my team to type in the "Label" of a person and have the adjacent cell to show up the corresponding ID.
Given that sheet 1 is something like:
ID Label
01 Alice Johnson
02 Bert Junger
03 Carlos Lopez
I would like that in sheet 2 would happen something like:
person = raw_input()
if(person == exist in sheet 1):
get person's col and row from sheet 1
return value(person's col-1, person's row)
Ideally, I'm looking to do this only with google spreadsheets formulae, since it would be otherwise hard for my colleagues to be able to work on it.
Thank you so much everyone!
You could do this with an Index Match formula or V Lookup.. Have a look at this spreadsheet.