AWS Quicksight: Comparing one string column against another - amazon-quicksight

Trying to build a calculated field in Quicksight so i can compare one string column against another and return a Boolean/integer if it matches, basically the equivalent of this in Power BI:
IF (
CONTAINSSTRING ( 'Owners Reg'[Registered Owner],'Owners Reg'[Alias_AKA.1] )
|| CONTAINSSTRING ( 'Owners Reg'[Alias_AKA.1], 'Owners Reg'[Registered Owner])
,
"Yes",
"No"
)
I've found the locate function but I'm struggling to see how i can use it. I was thinking something along the lines of
locate({Registered Owner},{Alias_AKA.1})
but i can't find any example of locate being used in the way i want to. Can anyone please help point me in the right direction as i'm a new user to AWS QS coming from Power BI

You are almost there, just need to add another function - ifelse
Following can be used -
ifelse(locate({String-1},{String-2}) > 0,'yes','no')
Refer sample data-set and function result below -

Related

DAX - CONCATENATEX TO FILTER AN UNRELATED TABLE

I have an unrelated table ('Selected Values Slicer') that is then used as a slicer to select values. I need to use these selected values to filter another table ('Results Table').
Doesn't seem to be working as I'd expect it to. The output shows a 0 when more then 1 item is selected but works ok when 1 value is selected
VAR __SelectedValues =
CONCATENATEX(
ALLSELECTED('Selected Values Slicer'[Description]),
'Selected Values Slicer'[Description],
",")
RETURN
CALCULATE(
[TotalCount],
'Results Table'[Description] IN {__SelectedValues}
)
__SelectedValues RETURNS "Selected Value 1,Selected Value 2"
What I'm expecting is __SelectedValues to RETURN "Selected Value 1","Selected Value 2"
The following syntax will return "Selected Value 1","Selected Value 2" as you asks
CONCATENATEX(
ALLSELECTED('Selected Values Slicer'[Description])
,"""" & 'Selected Values Slicer'[Description] & """"
,","
)
Regarding filtering. I was trying to use the string as filter, but I didn't manage it the way you want. I can't say what is the reason for DAX not to do that, but asked Marco Russo about the case. So, I'll add the answer since a get it (upd. the answer added to the end of the answer).
To filter the table with a string of CONCATENAX() you can try the following sytax ( I checked it with a dummy data):
VAR __SelectedValues =
CONCATENATEX(
ALLSELECTED('Selected Values Slicer'[Description])
,'Selected Values Slicer'[Description]
,","
)
RETURN
CALCULATE(
[TotalCount]
,FILTER(
'Results Table'
,CONTAINSSTRING(
__SelectedValues
,'Results Table'[Description]
)
)
)
Regarding result you want to achieve. A below code, I believe, is enough. You have a slicer and you want to apply its values to the result. I have a feeling, you don't need to change a contex to the slicer and no context can influence it. If it's not like that, then the code should be altered, but the idea remains the same - get a column with a unique values and use it as a filter. I checked the syntax and it was functioning.
CALCULATE(
[TotalCount]
,'Results Table'[Description] IN VALUES('Selected Values Slicer'[Description])
)
P.S.
My first answer was not ok. I messed some info and gave an incorrect input. I'm sorry for that.
P.S.S. The answer from Marco Russo:
"IN looks for an exact match in a table (IN VALUE is a better choice for your need, and a better one is:
TREATAS ( VALUES ( slicer[column1] ), table[color] )
The result of CONCATENATEX is a string, so you should use CONTAINSSTRING - DAX Guide to do the search, but it would be slower and potentially inaccurate."
This is the answer from his colleague

Power BI DAX measure: Count occurences of a value in a column considering the filter context of the visual

I want to count the occurrences of values in a column. In my case the value I want to count is TRUE().
Lets say my table is called Table and has two columns:
boolean value
TRUE() A
FALSE() B
TRUE() A
TRUE() B
All solutions I found so far are like this:
count_true = COUNTROWS(FILTER(Table, Table[boolean] = TRUE()))
The problem is that I still want the visual (card), that displays the measure, to consider the filters (coming from the slicers) to reduce the table. So if I have a slicer that is set to value = A, the card with the count_true measure should show 2 and not 3.
As far as I understand the FILTER function always overwrites the visuals filter context.
To further explain my intent: At an earlier point the TRUE/FALSE column had the values 1/0 and I could achieve my goal by just using the SUM function that does not specify a filter context and just acts within the visuals filter context.
I think the DAX you gave should work as long as it's a measure, not a calculated column. (Calculated columns cannot read filter context from the report.)
When evaluating the measure,
count_true = COUNTROWS ( FILTER ( Table, Table[boolean] = TRUE() ) )
the first argument inside FILTER is not necessarily the full table but that table already filtered by the local filter context (including report/page/visual filters along with slicer selections and local context from e.g. rows/column a matrix visual).
So if you select Value = "A" via slicer, then the table in FILTER is already filtered to only include "A" values.
I do not know for sure if this will fix your problem but it is more efficient dax in my opinion:
count_true = CALCULATE(COUNTROWS(Table), Table[boolean])
If you still have the issue after changing your measure to use this format, you may have an underlying issue with the model. There is also the function KEEPFILTERS that may apply here but I think using KEEPFILTERS is overcomplicating your case.

DAX Filter function parameter from a measure

I'm trying to set up a custom filter using DAX in Power BI.
The idea is to use a parameter/value extracted from a Slicer.
So far, I was able to get the selected value from the Slicer, but I was not able to use it in a filter.
The filter expression works perfectly when I manually type the parameter "value", I mean:
FILTER(BI_PRD_MAPA_TRD, BI_PRD_MAPA_TRD[ORIG_GRUPO]="5DSL" || BI_PRD_MAPA_TRD[DEST_GRUPO]="5DSL")
Works just fine, but:
FILTER(BI_PRD_MAPA_TRD, BI_PRD_MAPA_TRD[ORIG_GRUPO]=[Measure] || BI_PRD_MAPA_TRD[DEST_GRUPO]=[Measure])
doesn't work.
The measure is working fine, as I created a "dummy" card to test it.
The measure itself is calculated by a DAX SELECTEDVALUE function:
Measure = SELECTEDVALUE(Tabela[GRUPO], "TODOS")
Can someone help me at this topic?
Get the measure value in a VAR in this DAX and use inside to it, as below.
VAR _SelectedValue = [Measure]
RETURN
FILTER(BI_PRD_MAPA_TRD, BI_PRD_MAPA_TRD[ORIG_GRUPO]=_SelectedValue || BI_PRD_MAPA_TRD[DEST_GRUPO]=_SelectedValue )
I believe the selection is single select not multiple.

New Column or Measure for NAICS ID based on first two numbers

Use first two digits of Column to give a name to a new column.
I have a list of companies and their NAICS ID. I would like to filter these into a pie chart but I don't want the 90000 different names (just the general ex. Agriculture or Mining). I want to utilize the first two digits in for the column to identify its general name. I am trying to use the DAX expression Switch to get this started. Is there a filter to do this within PowerBI?
I haven't started yet since I am not sure if this is possible.
You could simply create a calculated column based off of the original NAICS code using the following:
FirstTwoDigitsOfNAICS :=
SWITCH (
TRUE (),
LEFT ( 'Table'[NAICSCode] ) = x, "Something",
LEFT ( 'Table'[NAICSCode] ) = y, "Something Else"
)
This DAX will simply pull the first two characters from the entire code.

How to represent distances between two coordinates

I have a simple DataSet with 2 different coordinates by record.
(dem_latitude & dem_longitude are one coordinate, html_latitude & html_longitude are the other coordinate)
My main purpose is to see how big are the distances between each couple of coordinates.
As an clarification I don't want to see the distance as a number but represented in the map with the 2 coordinates (that form a couple) easily recognisable.
Solutions I can image but I don't know how to develop:
I can see for each coordinate which is its partner, maybe through a line.
Events like clicking in one coordinate makes the partner coordinate to be highlighted.
Any suggestion of how to implement any of this solutions or any other suggestion to achieve my "main purpose"?
If the solution is using the WYSIWYG CartoDB editor better :)
CartoDB UI and CartoDB WYSIWYG are very good and intuitive but when working with a single geometry per row. I'm not an expert, but I think in this case you'll need to use your developer super-powers and:
1 - alter the table schema and add a new column distance
2 - run a simple query to update that column value based on the points information:
UPDATE <table_name> SET distance = (SELECT ST_Distance(
ST_GeographyFromText(
'SRID=4326;POINT(' || dem_longitude || ' ' || dem_latitude || ')'
),
ST_GeographyFromText(
'SRID=4326;POINT(' || html_longitude || ' ' || html_latitude || ')'
)
)
);
Remember you can run queries using the API, so it's easy to integrate this query in any callback that adds data to the table to keep it updated.
Inspired by the #blat's answer, and after dealing with a lot of weird errors with SRIDs codes and the_geom_webmercator vs the_geom, I got this solution:
select
st_transform(
st_makeline(
st_setsrid(st_makepoint(html_longitude,html_latitude), 4326),
st_setsrid(st_makepoint(dem_longitude,dem_latitude), 4326)
),
3857
) as the_geom_webmercator
from the_data_set
If you also want to show the initial and the end point you have to create extra layers for each coordinate couple and then do something like:
select
cartodb_id,
st_transform(
st_setsrid(st_makepoint(dem_longitude,dem_latitude), 4326),
3857
) as the_geom_webmercator
from the_data_set
The result looks like this:

Resources