I work on Power BI and I have trouble to calculate what I want.
I calculated the indicator Valorisation (an amount of euro) who varies every day. The following provides an overview with a filter on the date (between 26/06/2021 and 30/06/2021) :
Date
Valorisation
26/06/2021
988
27/06/2021
845
28/06/2021
802
29/06/2021
720
30/06/2021
699
I would like to add a third column which contains uniquely the value of the Valorisation of the first date.
Example 1 :
With a filter on the date between 26/06/2021 and 30/06/2021, I would like that :
Date
Valorisation
NewColumn
26/06/2021
988
988
27/06/2021
845
988
28/06/2021
802
988
29/06/2021
720
988
30/06/2021
699
988
Example 2 :
With a filter on the date between 27/06/2021 and 30/06/2021, I would like that :
Date
Valorisation
NewColumn
27/06/2021
845
845
28/06/2021
802
845
29/06/2021
720
845
30/06/2021
699
845
I don't manage to create the measure NewColumn. I tried several things without success like that :
NewColumn =
CALCULATE(
[Valorisation Stock PVTTC N],
DATESBETWEEN( Calendrier[DAT_REF],min(Calendrier[DAT_REF]),min(Calendrier[DAT_REF]) )
)
Thanks for your help.
Can you please try something as below-
NewColumn =
var selected_min_date = MIN(table_name[Date])
RETURN
CALCULATE(
sum[Valorisation],
FILTER(
ALL(table_name),
table_name[Date] = selected_min_date
)
)
I adapted the code of mkRabbani and it worked ! Thanks !
The code :
NewColumn =
var selected_min_date = CALCULATE(MIN(Calendrier[DAT_REF]),ALLSELECTED(Calendrier[DAT_REF]))
RETURN
CALCULATE(
[Valorisation Stock PVTTC N],
FILTER(
ALL(Calendrier),
Calendrier[DAT_REF] = selected_min_date
)
)
Related
I have a problem with the measure of the 3mth rolling average to visualise it correctly on the graph.
The data model is here:
https://docs.google.com/spreadsheets/d/1naChcuZtjSbk0pVEi1xKuTZhSY7Rpabc0OCbmxowQME/edit?usp=sharing
I am using the formula below to calculate 3mth average through a measure:
Product3Mth = CALCULATE(SUM('Table'[Product A uncum]);DATESINPERIOD('Table'[Date];LASTDATE('Table'[Date]);-3;MONTH))/3
When I am plotting it as a table it is showing right values for each month.
But When I am plotting it in the column chart together with Product A Accumulated I am getting wrong value which is the value for Product unaccum /3 insted of sum of 3 consecutive values for Product unaccum /3.
What should I change in the DAX to have it visualised correctly? Please HELP
It seems you've stumble accross quite a few Power BI "gotchas" here when it comes to both the format of the date in your source data and the way you've chosen to display the Date column in your visulaization. But I think I've figured it out. This is my result:
And just to verify some numbers:
(4043 + 20 + 158) / 3 = 1469
(189+ 200 + 207) / 3 = 199
And here are the details:
I used this dataset where I've changed the names slightly to make it easier to write DAX expressions and imported it using Get Data
Date unAcc ACc
01-10-2017 00:00 4043 4043
01-11-2017 00:00 205 4248
01-12-2017 00:00 158 4406
01-01-2018 00:00 142 4548
01-02-2018 00:00 312 4860
01-03-2018 00:00 258 5118
01-04-2018 00:00 176 5294
01-05-2018 00:00 210 5504
01-06-2018 00:00 189 5693
01-07-2018 00:00 200 5893
01-08-2018 00:00 207 6100
And for reasons still uknown to me, I had the same issues as you had with the Date column. But following some tips from the Power BI community, I created a Date2 like this :
Date2 =
DATE('Table1'[Date].[Year];'Table1'[Date].[MonthNo];1)
Then I calculated the three month average using a
Moving_Average_3_Months =
CALCULATE (
AVERAGEX ( 'Table1'; 'Table1'[unAcc] );
DATESINPERIOD (
'Table1'[Date2];
LASTDATE ( 'Table1'[Date2]);
-3;
MONTH
)
)
Now, if you insert a column chart and assign Date2 to the Axis and Moving_Average_3_months together with unAcc to Values, you'll get this:
And that's not what we want. So go to the Visualization settings and change Date2 from Date Hierarchy to simply Date2 like this:
And that's it:
And here's the whole thing as a table so you can see that the numbers are correct:
In your case, maybe the only thing you have to do is that very last part.
Please don't hesitate to let me know how it works out for you!
I ask in the last comment the issue about the calculated value (as column or measure) because I have different results based on this, as you can see in the example below:
The Product3Mth is based on the calculated column and the Product3Mth 2 is based on the measure (result you want!).
Hope this example can help you. If not tell me please!
The DATESINPERIOD and LASTDATE functions will not work correctly, because 'Table'[Date] is not a continous range of dates. These functions need to be based on a datetable.
Try something like this
Product3MthAVG =
VAR rowDate = 'Table'[Date]
RETURN
CALCULATE (
SUM ( 'Table'[Product A unaccum] );
FILTER (
'Table';
'Table'[Date]
< DATE ( YEAR ( rowDate ); MONTH ( rowDate ) + 1; DAY ( rowDate ) )
&& 'Table'[Date]
>= DATE ( YEAR ( rowDate ); MONTH ( rowDate ) - 2; DAY ( rowDate ) )
)
)
/ 3
Excel file(104976x10) includes large data.
A column: Time (unit year)
B column: Year
C column: Day of the year
D column: Hour
E column: Minute
and others including values
I would like to convert column which begins with B column until E column to date format like 'dd/mm/yyyy HH:MM'.
Example for the data:
1998,41655251 1998 152 1 0 12,5 12,0 11,8 11,9 12,0
I would like to do date instead of 2-th, 3-th, 4-th and 5-th columns.
1998,41655251 01/06/1998 01:00 12,5 12,0 11,8 11,9 12,0
or
1998,41655251 01/06/1998 01:00 1998 152 1 0 12,5 12,0 11,8 11,9 12,0
Welcome to SO.
Matlab has two types of date-format:
datetime, introduced in 2014b.
datenum, introcuced in long ago (before 2006b), it is basically a double precision value giving the number of days from January 0, 0000.
I think the best way is to use datetime, and give it the year, month, day, hour and minute values like this:
t=datetime(1998,0,152,1,0,0)
t= '01-May-1998 01:00:00'
As you can see the days automatically overflow into the months. But I end up 1st of may, not 1st of june like in your example.
to change the format:
t.Format='dd/MM/yyyy hh:mm'
t= '01/05/1998 01:00'
to convert it to a string, you can simply use string(t)
This is an example that combines the above functions to read an xlsx file and writes a new one with the updated column.
data=xlsread('test.xlsx');
S = size(data);
t = datetime(data(:,2),0,data(:,3),data(:,4),data(:,5),0);
t.Format='dd/MM/yyyy HH:mm';
data2=num2cell(data(:,1));
data2(:,2)=cellstr(string(t));
data2(:,3:S(2)-3)=num2cell(data(:,6:end));
xlswrite('test2.xlsx',data2);
Part A:
Define a macro variable for the quarter number. The idea is that this is the only thing the "user" should have to change when running the program for a new quarter.
Part B:
Define macro variables for each month in the quarter and set them equal to a month value that is generated from the quarter number. Hint: %if/%then
Given code:
data Month1;
input Name $ sales;
cards;
Joyce 235
Marsha 352
Bill 491
Vernon 210
Sally 418
;
data Month2;
input Name $ sales;
cards;
Joyce 169
Marsha 281
Bill 315
Vernon 397
Sally 305
;
data Month3;
input Name $ sales;
cards;
Joyce 471
Marsha 314
Bill 394
Vernon 291
Sally 337
;
data Month4;
input Name $ sales;
cards;
Joyce 338
Marsha 259
Bill 310
Vernon 432
Sally 362
;
data Month5;
input Name $ sales;
cards;
Joyce 209
Marsha 355
Bill 302
Vernon 416
Sally 475
;
data Month6;
input Name $ sales;
cards;
Joyce 306
Marsha 472
Bill 351
Vernon 405
Sally 358
;
proc sql;
create table qtr1 as
select Month1.name, month1.sales as m1sales, month2.sales as m2sales,
month3.sales as m3sales, sum(month1.sales, month2.sales, month3.sales) as qtr1sales
from month1, month2, month3
where month1.name=month2.name=month3.name;
select sum(m1sales) as m1total, sum(m2sales) as m2total, sum(m3sales) as m3total,
sum(qtr1sales) as qtr1total
from qtr1;
My solution:
/* question a */
%MACRO qtrn(qtr);
proc print data=&qtr ;
run;
%MEND qtrn;
/* question b */
%Macro Firstqtr(qtr);
%Let I = 1;
%If &qtr = qtr1 %then %do %until (&I > 3);
%Let var&I = Month&I;
%let I = %eval(&I + 1);
%end;
%Mend Firstqtr;
%Firstqtr(qtr);
Can anyone help me figure correct solution?
since this looks like a homework problem, here's the main part of your answer. I'll leave the final select for you to add. Should be pretty simple given the following solution:
%macro qtrSales(qtr);
%do i = 1 %to 3;
%let month&i = month%sysevalf((&qtr-1) * 3 + &i);
%put &&month&i;
%end;
proc sql;
create table qtr&qtr as
select &month1..name,
&month1..sales as &month1.sales,
&month2..sales as &month2.sales,
&month3..sales as &month3.sales,
sum(&month1..sales, &month2..sales, &month3..sales) as qtr&qtr.sales
from &month1, &month2, &month3
where &month1..name=&month2..name=&month3..name;
select sum(&month1.sales) as &month1.total,
sum(&month2.sales) as &month2.total,
sum(&month3.sales) as &month3.total,
sum(qtr&qtr.sales) as qtr&qtr.total
from qtr&qtr;
select sum(&month1.sales) as &month1.total,
sum(&month2.sales) as &month2.total,
sum(&month3.sales) as &month3.total,
sum(qtr&qtr.sales) as qtr&qtr.total
from qtr&qtr;
quit;
%mend qtrSales;
%qtrSales(2);
define a macro variable means simply to use %let to define a macro variable. macro variables are things that you define with %let, call symputx, or select into in SQL, and then reference using &.
%let qtrn = 3;
There you go. The question specified that the user will adjust this, right? So it isn't asking you to do any work on your end, just give the user a place to make this change.
As for the second, I don't entirely understand the hint. It doesn't seem necessary to use conditional logic here. Here's an example of what I'd do.
%let month1 = %eval(3*(&qtrn.-1)+1);
That simply calculates the month number of the first month based on the quarter. Quarter 3 is months 7/8/9, right? 3*(3-1)+1 = 7, 3*(3-1)+2 = 8, 3*(3-1)+2 = 9. (Or you could do it differently, 3*3-2 = 7, 3*3-1 = 8, 3*3 = 9)
Of course, you could do this in a macro with a loop to define them. But it seems excessive to do so - it's not like quarters ever have 4 months in them, or 2, right? They always have 3, it's a defining characteristic of a quarter, so it seems fine to hardcode month1/month2/month3.
I have one table and group the records using formula, based on a string field which is formed as time (HH:mm:ss)
Formula is as followings:
select Minute (TimeValue ({MASTER.Saat}))
case 0 to 14: ReplicateString ("0", 2-len(TOTEXT(Hour (TimeValue ({MASTER.Saat})),0))) & TOTEXT(Hour (TimeValue ({MASTER.Saat})),0) & ":00:00"
case 15 to 29: ReplicateString ("0", 2-len(TOTEXT(Hour (TimeValue ({MASTER.Saat})),0))) & TOTEXT(Hour (TimeValue ({MASTER.Saat})),0) & ":15:00"
case 30 to 44: ReplicateString ("0", 2-len(TOTEXT(Hour (TimeValue ({MASTER.Saat})),0))) & TOTEXT(Hour (TimeValue ({MASTER.Saat})),0) & ":30:00"
case 45 to 59: ReplicateString ("0", 2-len(TOTEXT(Hour (TimeValue ({MASTER.Saat})),0))) & TOTEXT(Hour (TimeValue ({MASTER.Saat})),0) & ":45:00"
Actually, grouping works fine but my problem is that if there is no data in the table for a period, I can not show that in the report.
As an example;
Let my data has 5 records as following:
11:01:03
11:16:07
11:28:16
12:18:47
12:22:34
My report gives the result as following:
Period | Total Records
11:00:00 | 1
11:15:00 | 2
12:15:00 | 2
In this situation, I can not show the periods (which are missing in the table) as 0 for Total Records. I have to show as follows:
Period | Total Records
11:00:00 | 1
11:15:00 | 2
11:30:00 | 0
11:45:00 | 0
12:00:00 | 0
12:15:00 | 2
Thanks for all suggestions.
You can't group something that's not there. One way to solve this is to use a table that provides all of your intervals you want to look at (called a date, time or number table).
For your case create a table that contains all your period values (24x4). join the records you want to count to this table. In Crystal Reports group by the period values - your result set will contain the periods without any joined records - you can detect this and output a 0.
You may want to look a this question, it is similar to yours.
I have a problem which seems to be very simple to solve but I can't. In my Fact table I have a Timestamp field which is a smalldatetime Type. This fact is linked to a Time dimension via its fulldate_Fk (also SmallDatetime). So What I would like to have is to compare the timestamp with the FullDate_FK from the fact to create a calculation like this:
iif([Dim Time].[Date].CurrentMember.MemberValue <=
[Fact].[Timestamp].CurrentMember.MemberValue
,[measures].[YTD Actuals]
,[measures].[YTD Actuals]+[measures].[YTD Com])
But it is not working at all. All [Dim Time].[Date] seem to be evaluated as < than the Timestamp.
P.S: The Timestamp is the last date when the data have been loaded in the DB (in my case 31/08)
Here the result I got:
MONTH | YTD Actuals | YTD Com | Calculation;
JAN , 10 , 10 , 10;
FEB , 20 , 10 , 20;
MAR , 40 , 20 , 40;
MAY , 60 , 30 , 60;
JUN , 70 , 50 , 70;
JUL , 85 , 50 , 85;
AUG , 120 , 55 , 120;
SEP , 120 , 60 , 120;
OCT , 120 , 70 , 120;
NOV , 120 , 80 , 120;
DEC , 120 , 90 , 120;
From August, I should have the sum of Actuals YTD and Com YTD in the calculation, but I still have the Actuals YTD only?
Extra Info
I'm using PivotTable just by dragging attributes in Excel. Month in rows and measures (the 2 YTD and the new calculated member)
If you build a new calc which is:
[Fact].[Timestamp].CurrentMember.MemberValue
What does it return when you add it to your PivotTable? Null? I suspect the CurrentMember is the All member so MemberValue is null. But let's test that.
Do all rows in the fact table have the same Timestamp or are there many different timestamps?
If your fact table has 10000 rows are you expecting the IIf calc will be evaluated 10000 times (once for each row)? That's not the way MDX works. In your PivotTable that has 12 rows the IIf calc gets evaluated 12 times at the month grain.
If you want the calculation to happen on each of the 10000 rows then write the calculation in SQL and do it in a SQL view before it gets to the cube.
To make the calc work as you intend in the cube consider doing the following. Add a new column in your DimTime SQL table called Today Flag. It should be updated during the ETL to be Y only on the row which is today and should be N on other rows. Then add that column as a new attribute to your Dim Time dimension. You can make it Visible=False. Then go to the Calculations tab and flip to the Script view and replace your current [Measures].[Calculation] calc with this:
Create Member CurrentCube.[Measures].[Calculation] as
[measures].[YTD Actuals];
Scope({Exists([Dim Time].[Month].[Month].Members,[Dim Time].[Today Flag].&[Y]).Item(0).Item(0):null});
[Measures].[Calculation] = [measures].[YTD Actuals]+[measures].[YTD Com];
End Scope;