How do I assimilate the division of wages per hour between permanent employee and intern in Dax? - powerbi

I'm trying to do it with IF, but it gives a syntax error.
Below is the measurement.
sal hour =
[salary per day]/8
IF(SELECTEDVALUE('RH[office]) = "Internship" / 6

Your IF function is not correct
The function is
IF (<Logical Test>, <Result if true> , <Result if false>)
Using my example:
Selected Value Check = [Hours] / IF(SELECTEDVALUE('Table'[Column2]) = "A", 6, 8)
You need to check IF it is internship is true, then return 6 else return 8
sal hour = [salary per day] / IF(SELECTEDVALUE('RH[office]) = "Internship", 6, 8)
You should use the DIVIDE function as well, as it will handle divide by zero.

Related

How to convert day of week from Sunday first to Monday first in c++?

I have a function which returns the day of week as an integer, Sunday = 0, Monday = 1, etc. to 6. I want to convert this to Monday = 0, ..., Sunday = 6. I can not think of better way than the one listed below, but it is clumsy. Is there a better way?
if (!currTime.dayOfTheWeek()) { // if time.dow == 0
dayOfWeek = 6;
}
else {
dayOfWeek = currTime.dayOfTheWeek() - 1;
}
by the way, this is Arduino code, using RTCLib for time.
Alternative:
To map 0...6 from [Sun. to Sat.] to [Mon. to Sun.]
dayOfWeek_fromMonday = (dayOfWeek_fromSunday + 6)%7;
Say you wanted to start on Wednesday (something more interesting that a shift by 1) rather than Sunday and avoid naked magic numbers.
#define DaysWedToSun 4
#define DaysPerWeek 7
dayOfWeek_fromWed = (dayOfWeek_fromSun + DaysWedToSun)%DaysPerWeek;
Keep it simple. What are the rules? If the day is Sunday(0) we change it to 6, all others we subtract one.
int dayOfWeek = ..;
dayOfWeek = dayOfWeek == 0 ? 6 : dayOfWeek - 1;

Rounding months to the correct year

I am creating an Age Dimension.
Granularity is months, so i have started by creating a column called TotalMonths, between 1 - 1440, which equates to 120 years.
I now want to add a year column.
I thought totalMonths/12 would work, but on month 6, the year rounds up to 1.
The year should only round up after 12.
Is there a way to do this in dax?
If you want to make a custom column in Power Query, then you can use Number.RoundDown function:
Year_M = Number.RoundDown([TotalMonths] / 12)
If you do it in DAX, then use ROUNDDOWN function:
Year_DAX = ROUNDDOWN(Age[TotalMonths] / 12, 0)
If you want first Year=1 to be for TotalMonth=13 instead, then subtract 1 from TotalMonth in the above calculations:
Year_M = Number.RoundDown(([TotalMonths] - 1) / 12)
Year_DAX = ROUNDDOWN((Age[TotalMonths] - 1) / 12, 0)

PowerBI - TIME function limitation problem

I have column with number of seconds and I'm trying to convert it to HH:mm:ss format.
All is working well if values are not empty or above 32767 since this is limitation of TIME function.
My question is: how can I still convert values above 32767 in HH:mm:ss format?
What I have so far is:
time_elapsed = IF(ISBLANK([time_in_sec]);"NaN";FORMAT(TIME(0;0;[time_in_sec]);"HH:mm:ss"))
But this not even checking because I don't know how to pass empty field as empty field and not Null or "Nan" or anything else when dealing with integer column.
For all other cases function FORMAT(TIME(0;0;[time_in_sec]);"HH:mm:ss") works well.
So 2 problems - how to convert numbers larger than 32767 to HH:mm:ss and how to just pass empty fields.
And in case of negative integer number it should return empty field as well.
1) It's possible that space character or another unprintable character may be present. In this case the value isn't considered BLANK. We need to see a sample of your data to tell exactly what's going on.
2) You can implement the formula, that converts seconds to the HH:MI:SS format, yourself, as follows:
// calculated column
hh_mi_ss =
VAR hr = FLOOR( query[column] / 3600, 1 ) // hours
VAR mn = FLOOR( MOD( query[column], 3600) / 60, 1) // minutes
VAR ss = FLOOR( MOD ( MOD( query[column], 3600) , 60 ), 1) // seconds
RETURN FORMAT( TIME(hr, mn,ss), "HH:mm:ss" )
FORMAT( DIVIDE( [DurationInSeconds] ) , 86400), "HH:mm:ss" )
This one-liner returns time part. It trims the day part if the DurationInSeconds is larger than 1 day (86400 seconds). If Duration is blank it returns blank.
Correct answer from #Nick Krasnov resolved my problem, I only needed to add IF function to regulate appearance of negative numbers, zeros and empty cells.
So I used:
hh_mi_ss = if([time_column]>0,(
VAR hr = FLOOR( [time_column] / 3600, 1) // hours
VAR mn = FLOOR( MOD( [time_column]; 3600) / 60, 1) // minutes
VAR ss = FLOOR( MOD ( MOD( [time_column], 3600) ,60 ), 1) // seconds
RETURN FORMAT( TIME(hr, mn,ss), "HH:mm:ss" ));
"Empty, 0 or negative value")
And in my locale I had to replace , with ; in argument of function.

ternary operator ?: is throwing errors about wrong arguments

I'm trying to display a different time frame macd on a given time frame chart. so display 5 min macd on 1 min chart etc.
I've decided to accomplish that by multiplying a number 5 to the interval which is an integer and then turn that into a string and use that in the plot.
This works fine since I don;'t have to change it every time I change the time frame of the chart from 1 to 10 min etc, and it will still display the longer time frame macd based on the multiple.
This following code works fine using the ternary operator ?:
//#version = 2
study(title="test")
source = close
fastLength = input(12, minval=1)
slowLength=input(26,minval=1)
signalLength=input(9,minval=1)
// res5 mutiplies the current interval which is an integer by a factor 5 and turns it into a string with the value of "interval*5" or "1D" depending on the value of interval*5
res5= interval*5 < 1440 ? tostring(interval*5) : "1D"
src5=security(tickerid, res5, close)
fastMA5 = ema(src5, fastLength)
slowMA5 = ema(src5, slowLength)
macd5 = fastMA5 - slowMA5
signal5 = sma(macd5, signalLength)
outMacD5 = security(tickerid, res5, macd5)
plot( outMacD5 ? outMacD5 : na, color= red)
But if I were to change it to have more conditions like below, the ternary operator fails.
//#version = 2
study(title="test")
source = close
fastLength = input(12, minval=1)
slowLength=input(26,minval=1)
signalLength=input(9,minval=1)
// res5 mutiplies the current interval which is an integer by a factor 5 and turns it into a string with the value of "interval*5" or "1D" depending on the value 9of inteval*5
//res5= interval*5 < 1440 ? tostring(interval*5) : "1D"
res5= interval*5 < 1440 ? tostring(interval*5) : interval >= 1440 and interval*5 < 2880 ? "1D":na
src5=security(tickerid, res5, close)
fastMA5 = ema(src5, fastLength)
slowMA5 = ema(src5, slowLength)
macd5 = fastMA5 - slowMA5
signal5 = sma(macd5, signalLength)
outMacD5 = security(tickerid, res5, macd5)
plot( outMacD5 ? outMacD5 : na, color= red)
That brings back the error
Add to Chart operation failed, reason: Error: Cannot call `operator ?:` with arguments (bool, literal__string, na); available overloads ...
Using the iff brings back the same error about the arguments being incorrect.
I could really use some help here. I'm so lost in using these conditional operators.
Any tips are helpful.
Use this:
res5= interval*5 < 1440 ? tostring(interval*5) : interval >= 1440 and interval*5 < 2880 ? "1D": ""
plotchar(res5=="5", "res5 test", "", location=location.top)
The plotchar() call will allow you to confirm res5's value. Here it is being tested for "5", so you will be able to verify in the Data Window (doesn't print anything in the indicator's pane so it doesn't disturb scale) that its value is 1 -> true when you are on a 1min chart.
[Edit 2019.08.19 09:02 — LucF]
Your question was about the ternary not working, which the code above resolves. Following your comment, you also need a more complete function to calculate a multiple of the current timeframe in v2. Use this:
f_MultipleOfRes( _mult) =>
// Convert target timeframe in minutes.
_TargetResInMin = interval * _mult * (
isseconds ? 1. / 60. :
isminutes ? 1. :
isdaily ? 1440. :
isweekly ? 7. * 24. * 60. :
ismonthly ? 30.417 * 24. * 60. : na)
// Find best way to express the TF.
_TargetResInMin <= 0.0417 ? "1S" :
_TargetResInMin <= 0.167 ? "5S" :
_TargetResInMin <= 0.376 ? "15S" :
_TargetResInMin <= 0.751 ? "30S" :
_TargetResInMin <= 1440 ? tostring(round(_TargetResInMin)) :
tostring(round(min(_TargetResInMin / 1440, 365))) + "D"
See here for a use case, but don't use that function code as it's v4.

Find the difference between the dates and group it under some category

I have a start date and an end date. I need to find the difference between these dates and group it under the following categories.
< 1 year, < 2 year and so on till X years.
I'm trying to write a unix C++ program for this problem.
I can easily find the unix time difference between start and end date and compare with the 1 year's time stamp (12 * 30 * 20 * 60 * 60) and so on.
Is there any C++ function that returns the difference in years given the start and end date? Also let's say, the difference is 8 years, I suppose I have to write conditions like this,
if((end_date - start_date) < 12 * 30 * 24 * 60 * 60)
group = " less than 1 year"
...
...
Until what point do I stop at, as I won't know what the maximum difference is between the dates?
Is there any easy way to compute this?
I know i'm confusing here, but i ve put all my efforts to explain the problem here. Thanks in advance.
Also note, this is not a assignment or anything.
Assuming "precise years" (in other words, all years are 365 days long) is not an issue, I would do something like this (counting the number of times each year happens in this case - since the original question doesn't really say WHAT to do with each year)
const int MAX_YEARS = 10;
const int YEAR_IN_SECONDS = 365 * 24 * 60 * 60;
std::array<int, MAX_YEARS+1> bins;
int years = static_cast<int>(difftime(end_date - start_date) / YEAR_IN_SECONDS);
// Outside of range, put it at the end of range...
// We could discard or do something else in this case.
if (years > MAX_YEARS)
{
years = MAX_YEARS;
}
bins[years]++; // Seen one more of "this year".
Obviously, what you do with "bins", and what/how you store data there really depends on what you actually are trying to achieve.
An alternative solution would be to use const double YEAR_IN_SECONDS = 365.25 * 24 * 60 * 60;, which would slightly better cover for leap-years. If you want to be precise about it, you'd have to find out if you are before or after each of the leapday in a particular year that is divisible by 4 (and keep in mind that there are special cases for years divisible by 100 and other rules at 400).
#include <chrono>
using years = std::chrono::duration<std::chrono::system_clock::rep, std::ratio<365 * 24 * 60 * 60, 1>>;
std::chrono::system_clock::time_point end_date = std::chrono::system_clock::now();
std::chrono::system_clock::time_point start_date = end_date - years(2);
years how_many = std::chrono::duration_cast<years>(end_date - start_date);
int how_many_as_int = how_many.count();
std::cout << how_many_as_int << std::endl;
std::unordered_map<int, std::list<whatever>> m;
m[how_many_as_int].push_back(...);