How to convert Excel formula to Dax formula. I want to find out sum qty of previous day hour 24 if the current hour is 1 .
Below is the example using excel formula.
=IF(E3=1,SUMIFS($F$3:$F$17,$C$3:$C$17,D3,$E$3:$E$17,24),0)
date,previous date,hour,qty,Formula
10/10/2016,10/09/2016,24, 5,0
10/11/2016,10/10/2016, 1, 1,8
10/12/2016,10/11/2016,24, 2,0
10/13/2016,10/12/2016,24, 2,0
10/13/2016,10/12/2016, 3, 2,0
10/14/2016,10/13/2016, 1, 2,32
10/14/2016,10/13/2016, 1, 2,32
10/14/2016,10/13/2016,24,10,0
10/14/2016,10/13/2016, 4,10,0
10/14/2016,10/13/2016,24,10,0
10/13/2016,10/12/2016,24,10,0
10/13/2016,10/12/2016, 1,10,2
10/13/2016,10/12/2016,24,10,0
10/13/2016,10/12/2016,24,10,0
10/10/2016,10/09/2016,24, 3,0
Let's say your table is called Table1.
Create a calculated column with this formula:
CALCULATE(SUM(Table1[qty]),FILTER(Table1,[date] = (EARLIER([date]) - 1) &&
Table1[hour]=24 && EARLIER([hour]) = 1))
This will replicate the results you were able to calculate using SUMIFS() in Excel.
Now, you can totally use this formula and run with it, but the slight caveat is that it returns blank instead of 0 for cases where the formula does not apply. In order to handle that, you can use a blank check in DAX, like so:
=var x =
CALCULATE(SUM([qty]),FILTER(Table1,[date] = (EARLIER([date]) - 1) && Table1[hour]=24
&& EARLIER([hour]) = 1))
RETURN IF(ISBLANK(x),0,x)
Here's a screen of the results, with the calculated column on the right:
Related
I want to use Power Query to extract by field(field is [Project]), then get the top 3 scoring rows from the master table for each project, but if there are more than 3 rows with a score of over 15, they should all be included. 3 rows must be extracted every time as minimum.
Essentially I'm trying to combine Keep Rows function with my formula of "=if(score>=15,1,0)"
Setting the query to records with score greater than 15 doesn't work for projects where the highest scores are, for example, 1, 7 and 15. This would only return 1 row, but we need 3 as a minimum.
Setting it to the top 3 scores only would omit rows in a table where the highest scores are 18, 19, 20
Is there a way to combine the two function to say "Choose the top 3 rows, but choose the top n rows if there are n rows with score >= 15
As far as I understand you try to do following (Alexis Olson proposed very same):
let
Source = Excel.CurrentWorkbook(){[Name="Table"]}[Content],
group = Table.Group(Source, {"Project"}, {"temp", each Table.SelectRows(Table.AddIndexColumn(Table.Sort(_, {"Score", 1}), "i", 1, 1), each [i]<=3 or [Score]>=15)}),
expand = Table.ExpandTableColumn(group, "temp", {"Score"})
in
expand
Or:
let
Source = Excel.CurrentWorkbook(){[Name="Table"]}[Content],
group = Table.Group(Source, {"Project"}, {"temp", each [a = Table.Sort(_, {"Score", 1}), b = Table.FirstN(a, 3) & Table.SelectRows(Table.Skip(a,3), each [Score]>=15)][b]}),
expand = Table.ExpandTableColumn(group, "temp", {"Score"})
in
expand
Or:
let
Source = Excel.CurrentWorkbook(){[Name="Table"]}[Content],
group = Table.Group(Source, {"Project"}, {"Score", each [a = List.Sort([Score], 1), b = List.FirstN(a,3)&List.Select(List.Skip(a,3), each _ >=15)][b]}),
expand = Table.ExpandListColumn(group, "Score")
in
expand
Note, if there are more columns in the table you want to keep, for first and second variants you may just add these columns to last step. For last variant you haven't such option and the code should be modified.
Sort by the Score column in descending order and then add an Index column (go to Add Column > Index Column > From 1).
Then filter on the Index column choosing to keep values less than or equal to 3. This should produce a step with this M code:
= Table.SelectRows(#"Added Index", each [Index] <= 3)
Now you just need to make a small adjustment to also include any score 15 or greater:
= Table.SelectRows(#"Added Index", each [Index] <= 3 or [Score] >= 15)
I am building a function which will allow the user to choose a cell in a dynamically created table, by passing in an integer.
I.e., if I have a 3x3 grid, and the user passes in the number 4, the program should know that he wants the 1st cell in the second row. (the cells will be counted by rows)
As I mentioned, the table is created dynamically and can be any size.
I can do this with a bunch of if statements, but I was wondering if there is maybe an algorithm to figure this out easily.
(P.S. I am using a very basic programming language, so please, no fancy pythonic math functions... :) )
All you need to do is divide by columns and find the remainder. Something like this:
input = 4
row = floor(input / columns)
column = input % columns
Assuming the output should be 1 based index
input = n, k // n * n grid, k number
row = floor( (k - 1) / n ) + 1;
col = k % n;
if(col == 0) {
col += n;
}
// print row, col
Create a decision table to help the Municipal Bank decide whether or not to loan money to a customer. Include the criteria used by the bank to identify qualified applicants.
Conditions
Income >=40000? T T T F F F
Credit Score >=600? T F F T T F
Months at job > 12? - T F T F -
Outcomes
Approve loan? Y Y X Y X X
Use pseudo code to write an algorithm for the decision table created in question one.
If Income >= 4000 And credScore >= 600 And monthJob > 12 Then
loanApp = Yes
I am having trouble converting the table to pseudo code, I wanted to know if the partial answer to the second question is on the right track.
Generally the approach works, yes. Although if you look carefully, you will see that monthJob > 12 is not needed for the first column of your condiditions. (there is a '-' there)
A faster approach you can get if you investigate your conditions. We always get Y if 2 of the conditions are met, otherwise we get X.
So here a optimised version (pseudocode):
score = 0
If Income >= 40000 Then
score = score + 1
Endif
If credScore >= 600 Then
score = score + 1
Endif
If monthJob > 12 Then
score = score + 1
Endif
If score >= 2 Then
loanApp = Yes
Else
loanApp = No
EndIf
I have two matrices in Matlab A and B, which have equal number of columns but different number of rows. The number of rows in B is also less than the number of rows in A. B is actually a subset of A.
How can I remove those rows efficiently from A, where the values in columns 1 and 2 of A are equal to the values in columns 1 and 2 of matrix B?
At the moment I'm doing this:
for k = 1:size(B, 1)
A(find((A(:,1) == B(k,1) & A(:,2) == B(k,2))), :) = [];
end
and Matlab complains that this is inefficient and that I should try to use any, but I'm not sure how to do it with any. Can someone help me out with this? =)
I tried this, but it doesn't work:
A(any(A(:,1) == B(:,1) & A(:,2) == B(:,2), 2), :) = [];
It complains the following:
Error using ==
Matrix dimensions must agree.
Example of what I want:
A-B in the results means that the rows of B are removed from A. The same goes with A-C.
try using setdiff. for example:
c=setdiff(a,b,'rows')
Note, if order is important use:
c = setdiff(a,b,'rows','stable')
Edit: reading the edited question and the comments to this answer, the specific usage of setdiff you look for is (as noticed by Shai):
[temp c] = setdiff(a(:,1:2),b(:,1:2),'rows','stable')
c = a(c,:)
Alternative solution:
you can just use ismember:
a(~ismember(a(:,1:2),b(:,1:2),'rows'),:)
Use bsxfun:
compare = bsxfun( #eq, permute( A(:,1:2), [1 3 2]), permute( B(:,1:2), [3 1 2] ) );
twoEq = all( compare, 3 );
toRemove = any( twoEq, 2 );
A( toRemove, : ) = [];
Explaining the code:
First we use bsxfun to compare all pairs of first to column of A and B, resulting with compare of size numRowsA-by-numRowsB-by-2 with true where compare( ii, jj, kk ) = A(ii,kk) == B(jj,kk).
Then we use all to create twoEq of size numRowsA-by-numRowsB where each entry indicates if both corresponding entries of A and B are equal.
Finally, we use any to select rows of A that matches at least one row of B.
What's wrong with original code:
By removing rows of A inside a loop (i.e., A( ... ) = []) you actually resizing A at almost each iteration. See this post on why exactly this is a bad practice.
Using setdiff
In order to use setdiff (as suggested by natan) on only the first two columns you'll need use it's second output argument:
[ignore, ia] = setdiff( A(:,1:2), B(:,1:2), 'rows', 'stable' );
A = A( ia, : ); % keeping only relevant rows, beyond first two columns.
Here's another bsxfun implementation -
A(~any(squeeze(all(bsxfun(#eq,A(:,1:2),permute(B(:,1:2),[3 2 1])),2)),2),:)
One more that is dangerously close to Shai's solution, but still avoids two permute to one permute -
A(~any(all(bsxfun(#eq,A(:,1:2),permute(B(:,1:2),[3 2 1])),2),3),:)
I wish to ask if anybody out there knows how to partition an image into 8 different rows and 1 column? I have tried using mat2cell() and using the demo on their wiki as a reference, I tried partitioning the image into 8 rows, however not all image partition rows are displayed.
If you see the image below, 2, 4, 6, 8 is not displayed. I am also not sure why is it of 16 blocks.
Can somebody help me check my code? I am not really used to the MatLab syntax and language. I trying my best to understand now.
My code for splitting the blocks are as follows:
blockSizeR = 50; % Rows in block.
blockSizeC = 512; % Columns in block.
wholeBlockRows = floor(rows / blockSizeR);
blockVectorR = [blockSizeR * ones(1, wholeBlockRows), rem(rows, blockSizeR)];
wholeBlockCols = floor(columns / blockSizeC);
blockVectorC = [blockSizeC * ones(1, wholeBlockCols), rem(columns, blockSizeC)];
if numberOfColorBands > 1
% It's a color image.
ca = mat2cell(rgbImage, blockVectorR, blockVectorC, numberOfColorBands);
else
ca = mat2cell(rgbImage, blockVectorR, blockVectorC);
end
% Now display all the blocks.
plotIndex = 1;
numPlotsR = size(ca, 1);
numPlotsC = size(ca, 2);
for r = 1 : numPlotsR
for c = 1 : numPlotsC
fprintf('plotindex = %d, c=%d, r=%d\n', plotIndex, c, r);
% Specify the location for display of the image.
subplot(numPlotsR, 1, plotIndex);
% Extract the numerical array out of the cell
% just for tutorial purposes.
rgbBlock = ca{r,c};
imshow(rgbBlock); % Could call imshow(ca{r,c}) if you wanted to.
[rowsB columnsB numberOfColorBandsB] = size(rgbBlock);
% Make the caption the block number.
caption = sprintf('Block #%d of %d\n%d rows by %d columns', ...
plotIndex, numPlotsR*numPlotsC, rowsB, columnsB);
title(caption);
drawnow;
% Increment the subplot to the next location.
plotIndex = plotIndex + 1;
end
end
I am new to MatLab, so is there is a simpler method to do this that I missed out, please do suggest or better still, if there are references that I can refer to. Many thanks (:
If you know the dimensions of your matrix, you can do the math to figure out how to divide the number of rows into 4 equal parts:
e.g. If: size(rockinsMatrix) == [10 20] (a 10row x 20column) matrix,
then you could split it into a set of 4 sub-matrices, two with 3 rows, and 2 with 2 columns.
If you want the matrices in a cell array then you can do that at that time.
I managed to solve already, the error lies in the for loop. I changed the for r = 1 : numPlotsR into r = 1 : (number of rows I want) for c = 1 : numPlotsC into c= 1: 1(as I only want one column), and used subplot(8,1,k) or (8,2,k) where k is the plot index. Just answering this in case anybody encounter such problem in future and want to use my code as a reference. Cheers!