how to put a filter on magento collection on a computed column value - magento

How to add filter to collection to a computed column?
$collection->getSelect()->columns("sum(col1,col2) as distance ");
distance is computed from addition of col1 and col2
now how to place a check on distance<10 and reduce the number of items in the collection.
->addFieldToFilter('distance',array('gt'=>10)) didn't work
I have replaced a complex mathematical function with sum for simplicity

you can simply use having clause $collection->getSelect()->having('distance >10') directly in your query
take a look at this question https://magento.stackexchange.com/questions/6305/how-to-add-dynamic-field-in-magento-collection

Related

How to Select multiple related columns in add calculated fields in Quicksight parameter using ifelse?

I have a parameter 'type' in a table and it can have multiple values as follows -
human
chimpanzee
orangutan
I have 3 columns related to each type in the table -
human_avg_height, human_avg_weight, human_avg_lifespan
chimpanzee_avg_height, chimpanzee_avg_weight, chimpanzee_avg_lifespan
orangutan_avg_height, orangutan_avg_weight, orangutan_avg_lifespan
So if i select the type as human, the quicksight dashboard should only display the three columns -
human_avg_height, human_avg_weight, human_avg_lifespan
and should not display the following columns -
chimpanzee_avg_height, chimpanzee_avg_weight, chimpanzee_avg_lifespan
orangutan_avg_height, orangutan_avg_weight, orangutan_avg_lifespan
I created the parameter type and in the add calculated fields I am trying to use ifelse to select the columns based on the parameter selected as follows -
ifelse(${type}='human',{human_avg_height}, {human_avg_weight}, {human_avg_lifespan},{function})
I also tried -
ifelse(${type}='human',{{human_avg_height}, {human_avg_weight}, {human_avg_lifespan},{function}})
And -
ifelse(${type}='human',{human_avg_height, human_avg_weight, human_avg_lifespan},{function}})
But none of it is working. What am i doing wrong ?
One way to do this would be to use three different calculated fields, one for all the heights, one for weights and one for lifespan. The heights one would look like this:
ifelse(
${type}='human',{human_avg_height}, ifelse(
${type}='chimpanzee', { chimpanzee_avg_height}, ifelse(
${type}='orangutan',{ orangutan_avg_height},
NULL
)))
Make another calculated field for weights and lifespan and then add these calculated fields to your table, and filter by type.
To make it clear to the viewer what data is present, edit the Title of the visual to include the type:
${type} Data
You have to create one calculated field for each measure using the ifelse with the type to choose the correct vale, but is not necessary to create inner ifelse as skabo did, the if else syntax is ifelse(if, then [, if, then ...], else) so you can define the calculated fields as follows:
avg_height = ifelse(${type}='human', {human_avg_height}, ${type}='chimpanzee', {chimpanzee_avg_height},${type}='orangutan', {orangutan_avg_height}, NULL)
avg_weight = ifelse(${type}='human', {human_avg_weight}, ${type}='chimpanzee', {chimpanzee_avg_weight},${type}='orangutan', {orangutan_avg_weight}, NULL)
avg_lifespan = ifelse(${type}='human', {human_avg_lifespan}, ${type}='chimpanzee', {chimpanzee_avg_lifespan},${type}='orangutan', {orangutan_avg_lifespan}, NULL)
Then use those calculated fields in your visuals.

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.

Add custom formula to webdatarocks

Is it possible with webdatarocks to calculate % of difference per row for each period?
I want to add 3rd column and get % od difference for "Sum of foodDonorDelta" so for 30/09/2020 it will be null, for 31/10/2020 it will be 4000%, for 30/11/2020 it will be -78% and for 31/12/2020 it will be 1000%.
I Know I can set "% Difference" in fields options
but then I have to move "Values" to "Rows" while I need them in "Columns"
The %difference aggregation function of WebDataRocks has the following logic:
If the Values are in Rows, the %difference will be calculated based on the Sum row. In case Values are in Columns, the %difference will be calculated based on the Sum column.
So it is possible to calculate the %difference between Sum values per row, but only when the Values are in Rows.

How to sum all values for numberDisplay, excluding a category

I have set of data where I want to apply filters by default to a numberDisplay. The data is something like this.
data = [{category:'A',value:10},
{category:'B',value:10},
{category:'C',value:10},
{category:'S',value:10},
{category:'C',value:10},
{category:'A',value:10}]
I am trying to create a number display which will show sum of values other than category 'S', I tried using fake groups but they are failing. What would be the best method to achieve this ?
You don’t need a fake group for this, since you’re not trying to change the shape/structure of the aggregation. Ordinary crossfilter reductions cover this purpose.
You can simply do
cf.groupAll().reduceSum(d => d.category === ‘S’ ? 0 : d.value);
This will sum the value of every row included in the current filters, but will substitute zero if the row’s category is S.

PowerPivot DAX Max of two values

I have two columns and I need to extract the maximum value of those two for every row in my table. I have looked at the Max, Maxx and Maxa, but they all have input for just one column.
How would I write following expression in a Calculated column:
=max(
Table1[Column1],
Table1[Column2]
)
Actually, you should write the formula exactly as you described:
=max(
Table1[Column1],
Table1[Column2]
)
MAX function in dax exists in 2 versions: one takes a single column, the other takes 2 singular expressions.
Instead of MAX, you can just use a simple IF to achieve what you want:
= IF(Table1[Column1] >= Table1[Column2], Table1[Column1], Table1[Column2])

Resources