DAX: Averaging multiple % Columns - average

I'm new to Power BI and Dax, having some difficulty with the below scenario.
test a b c d AVERAGE
aa 51.97% 46.61% 49%
I have 4 columns, a-d, and I simply want the average of the 4 columns in the AVERAGE column. Dependent on the row different columns may be blank. Each of the columns are measures pulling through a % value into the table.
I'm sure there must be a simple solution to this but any help would be much appreciated.

Try creating a column like this:
AVERAGE = ([a]+[b]+[c]+[d])/4
UPDATE: BLANK measures don't affect average result.
AVERAGE = DIVIDE(([a]+[b]+[c]+[d]),
(IF(ISBLANK([a]),0,1) + IF(ISBLANK([b]),0,1) +
IF(ISBLANK([c]),0,1) + IF(ISBLANK([d]),0,1)))

Related

Tableau: Calculate the average value for the last 3 records

I have a time series data fro the different products. i like to calculate the average for each products based on only last 3 values. can you please help me.
Thanks
Kathir
Use SIZE() - INDEX() + 1
Drag it to your row or column and convert as discrete. Use table calc if needed
Now you will have an index for each row. Create another calculated field which will be using fix LOD of the above calculated field and when Index = 1 or 2 or 3, it sums it
You will be able to calculate average by diving the field with the sum of fix LOD created

Extracting last (non NaN) 200 columns from matrix with varying number of NaNs ending the rows

I have pupil size data from an eye tracking experiment. In the experiment, to start each trial the participant looks in the centre of the screen for 1000 ms (prefixation period), then the trial begins. If they look away or blink, the 1000 ms period restarts. So, each trial has a different length prefixation period. When I create a matrix (with each row being a different trial, and each column being a pupil size sample over time) it creates the matrix with the number of columns based on the trial with the longest prefixation period, and then adds varying numbers of NaNs to the end of each other row. I need to extract the last 200 samples (columns) of each trial, but these are not the last 200 columns of the matrix because of the additional NaNs that are added.
At the moment I have this:
Row1 = PreFixBase(1,:); % extract the first row
Row1(isnan(Row1)) = []; %get rid of the NaNs
Row1Base = Row1(end-200+1:end); %extract the last 200 samples / columns
which I do for each row separately and then paste them back together. It works, but is really inefficient (I have 324 rows / trials) and I'm sure there must be a more concise way of doing this, but haven't been able to find the answer.
Any help appreciated.
Amy
You can use cumsum + logical indexing to extract the desired elements
Base = PreFixBase.'; % transpose the matrix
S=cumsum(~isnan(Base),1,'reverse'); % number last non NaN columns from 1 to 200 (from end to begin)
Result = reshape(Base(S> 0 & S<=200),200,[]).'; % extract data and reshape to the correct size

Interactive Report - aggregate sum of multiple columns from one table multiply by values from another Table

I have a challenge in Oracle Apex - I would to sum multiple columns to give 3 extra rows namely points, Score, %score. There are more columns but I'm only choosing a few for now.
Below is an example structure of my data:
Town | Sector | Outside| Inside |Available|Price
Roy-----Formal----0----------0----------1------0
Kobus --Formal----0 ---------0--------- 1------0
Wika ---Formal----0----------0--------- 1------0
Mevo----Formal----1----------1----------1------0
Hoch----Formal----1----------1----------1------1
Points------------2----------2----------5------1
Score------------10---------10---------10------10
%score-----------20---------20---------50------10
Each column has a constant weighting (which serves as a factor and it can change depending on the areas). In this case, the weighting for the areas are in the first row of the sector Formal:
Sector |Outside| Inside |Available|Price
Formal----1----------1 ----------1-----1
Informal--1----------0 ----------2-----1
I tried using the aggregate sum function in apex but it wont work because I need the factor in the other table. This is where my challenge began.
To compute the rows below the report
points = sum per column * weighting factor per column
Score = sum of no of shops visited (in this case its 5) * weighting factor per column
% score = points/Score * 100
The report should display as described above. With the new computed rows below.
I kindly ask anyone to assist me with this challenge as I have tried searching for solutions but haven't come across any.
Thanks a lot for your support in advance!!

indexing to find corresponding number

I have a time series of measurements taken at different depths of a water column. I have divided these into individual cells (for later) and require some help on how to complete the following: e.g.
time = [733774,733774,733775,733775,733775,733776,733776];
bthD = [20,10,0,15,10,20,10];
bthA = (1000:100:1600);
%Hypsographic
Hypso = [(10:1:20)',(1000:100:2000)'];
d = [1,1.3,1,2.5,2.5,1,1.2];
data = horzcat(time',bthD',d');
uniqueTimes = unique(time);
counts = hist(time,uniqueTimes);
newData = mat2cell(data,counts,length(uniqueTimes));
So, in newData I have three cells, that correspond to different days of measurements, in each cell I have newData(:,1) being time, newData(:,2) being depth, and newData(:,3) being the measurement. I would like to find what the area is at each depth in the cells, the area at different depths is given in the variable 'Hypso'.
How could I achieve this?
Your problem formulation is excellent! Very easy to understand what you need here. All you need is the function interp1. Use the first column of Hypso, I assume, as your depth, and the second column as the area. You can use the vectorized ability of the interp1 function to find all values in one call:
areaAtDepth = interp1(Hypso(:,1),Hypso(:,2),bthD)
areaAtDepth =
Columns 1 through 6
2000 1000 NaN 1500 1000 2000
Column 7
1000
You'll notice the Nan in the third column of the output. This is because it's associated depth, 0, is outside the range of the data, or support of the data I believe. You'll need to decide what you want to do when data is outside the range, or perhaps it never should be, so an error should be logged; it's up to you! Let me know if you have any more questions!

How should I perform this binning and averaging in MATLAB?

I am trying to perform a binning average. I am using the code:
Avg = mean(reshape(a,300,144,27));
AvgF = squeeze(Avg);
The last line gets rid of singleton dimensions.
So as can be seen I am averaging over 300 points. It works fine except for times when I have a total number of points not equal to a multiple of 144*300.
Is there any way to make this binning average work even when the total number of points is not a multiple of 144*300?
EDIT: Sorry if my question sounded confusing. To clarify...
I have a file with 43200 rows and 27 columns. I am averaging by binning 300 rows at a time, which means in the end I am left with a matrix of size 144-by-27.
My code as I wrote it above works only when I have exactly 43200 rows. In some cases I have 43199, 43194, etc.. The reshape function works when I have a total number of rows that is a multiple of 300 (the bin size). Is there a way to make this binning average work when my total number of rows is not a multiple of 300?
I think I understand the problem better now...
If a is the data read from your file (of size N-by-27, where N is ideally 43,200), then I think you would want to do the following:
nRemove = rem(size(a,1),300); %# Find the number of points to remove
a = a(1:end-nRemove,:); %# Trim points to make an even multiple of 300
Avg = mean(reshape(a,300,[],27));
AvgF = squeeze(Avg);
This will remove points such that the number of rows in a will be a multiple of 300. Then your reshape and average should work. Note that I use [] in the call to RESHAPE, which lets it figure out what the number of column should be.