Public

# Conditional Average (average if)

Options
Registered Posts: 39 ✭✭✭✭

I want to calculate an average of all entries greater than 0 in a recipe. I am trying to do this in a group recipe. How would I do this? Is there a better way?

• Dataiker, Alpha Tester, Registered Posts: 138 Dataiker
Options

It treats them as blank, meaning that they won't be taken into account in your average computation.

• Dataiku DSS Core Designer, Dataiku DSS & SQL, Dataiku DSS ML Practitioner, Dataiku DSS Core Concepts, Neuron 2020, Neuron, Registered, Dataiku Frontrunner Awards 2021 Finalist, Neuron 2021, Neuron 2022, Frontrunner 2022 Finalist, Frontrunner 2022 Winner, Dataiku Frontrunner Awards 2021 Participant, Frontrunner 2022 Participant, Neuron 2023 Posts: 1,595 Neuron
Options

You don't say if you want to calculate this average over a bunch of rows, or across columns in a row. So, I can't be very specific here.

If your backend is based on a SQL besed server. You might be able to do something with a custom agrigation using a SQL case statement to count the non-zero values and a SQK Sum, and do the division for an average.

If you are working with "tall" or tidy style data the window recipe might give you an option to do what you want. It already knows how to do averages across groups of rows.

Hope that helps a bit. If not and you are willing to share a bit more about your data and use case. We might be able to help you a little bit better.

• Registered Posts: 39 ✭✭✭✭
Options

I have multiple columns that I want to compute averages for, grouped on timestep. Its economic scenario data so its like

Trial | Timestep | Asset A | Asset B | Asset C

I want to create columns that take the average at each timestep across all trials for Asset A where A is greater than 0, then again for B where B >0 and again for C where C > 0. I can take an average in the group recipe easily but its not "conditional". I have the option of using spark or DSS. I tried 'averageif' in spark but that did not work

• Dataiku DSS Core Designer, Dataiku DSS & SQL, Dataiku DSS ML Practitioner, Dataiku DSS Core Concepts, Neuron 2020, Neuron, Registered, Dataiku Frontrunner Awards 2021 Finalist, Neuron 2021, Neuron 2022, Frontrunner 2022 Finalist, Frontrunner 2022 Winner, Dataiku Frontrunner Awards 2021 Participant, Frontrunner 2022 Participant, Neuron 2023 Posts: 1,595 Neuron
Options

Thanks for sharing that you are using spark.

I don't have access to a Spark set up at the moment.

However, I'm wondering if you have set your visual recipe to use the SPARK Engine or if the recipe is running in the DSS engine. See below for how you can check.

If you have an averageif SPARK function. Then this might work.

As I've said I don't have access to a SPARK server. However, based on a quick Google Search I don't know if SPARK server actually provides an averageif function. In doing a quick search I'm only finding references to MS Excel when it comes to the function averageif.

Let us know how you get on with this. I'd like to invite anyone else here in the community with access to SPARK to pick up this thread and provide some additional help.

• Registered Posts: 39 ✭✭✭✭
Options

so I can change it back and forth from DSS to Spark (like how you have in the photo). The averageif does not work on spark. I am not sure how to do here. Do you know how I can accomplish my goal on DSS?

• Dataiker, Alpha Tester, Registered Posts: 138 Dataiker
Options

Hi,

If you want to apply a specific filter on your input data before performing the aggregation, you can use the "pre-filter" option on the left and select the "filter" category.

Best,

Harizo

• Registered Posts: 39 ✭✭✭✭
Options

but would this reject rows where conditions are not met in other columns?

In the below example I want to compute the average across all Trials at a given timestep. I want to reject the negative values BUT since the second row has a negative entry for B would it through away that row for all the other columns?

 Trial Time A B 1 0 .02 -.02 1 1 .02 .01 1 2 -.01 .03
• Dataiker, Alpha Tester, Registered Posts: 138 Dataiker
Options

Yes, the pre-filter operation removes the entire rows.

If you want to retain the rest of the values that are valid in a given row, you should consider running an upstream Prepare recipe on your Dataset and clear the cells that match your filtering criterion.

Best,

Harizo

• Registered Posts: 39 ✭✭✭✭
Options

how could I do that? does it clear them (treat as blank) or make them 0 (Which would lower average)

• Registered Posts: 39 ✭✭✭✭
Options

I found the function to do this. Thank you!

Help me…