how can i take the last value in month per month?
enter image description here
i need to take in this month - 2461 but my measure returm me blank .
this is my measure =
LastConversion_Repayments =
VAR maxDate =
CALCULATE (
MAX ( 'DimDate'[Date] ),
FILTER ( ALL ( 'DimDate' ),[sum_currentrepayments2] <> 0 )
)
var endofmonth1 = ENDOFMONTH(DimDate[Date])
var notblank = if(NOT(ISBLANK([sum_currentrepayments2])),1,0)
RETURN
IF (
notblank = 0,
BLANK (),
CALCULATE (
[sum_currentrepayments2],
FILTER ( ALL('DimDate'), 'DimDate'[Date] = endofmonth1 )
)
)
what i do wrong ???
Thx a lot!
i need to take in this month - 2461 but my measure returm me blank .
Related
SUM(B.RefundedAmount) + SUM(CASE WHEN B.AccountTypeID = 2 AND B.TotalStake = B.TotalPayout THEN B.TotalPayout ELSE 0 END)
The following is the logic for SQL code that I need to convert into a new powerBI column/measure.
RefundedAmount, AccountTypeID, TotalStake and TotalPayout are all columns in the dataset. I'm struggling to wrap my head around the addition aspect along with the IF/CASE WHEN statement.
Does it only mean that IF (AccountTypeID = 2 AND TotalStake = TotalPayout) then we return RefundedAmount + TotalPayout, otherwise return 0?
The measure always returns at least RefundedAmount, but if AccountTypeID = 2 and TotalStake = TotalPayout then it returns the sum of RefundedAmount and TotalPayout.
The measure could be written in DAX as:
Measure :=
VAR _refund = SUM ( 'Table'[RefundedAmount] )
VAR _payout =
SUMX (
FILTER (
'Table' ,
'Table'[TotalPayout] = 'Table'[TotalStake]
&& 'Table'[AccountTypeID] = 2
),
'Table'[TotalPayout]
)
RETURN
_refund + _payout
I have the following code written for a larger report that I have been working on. I have three columns resulting from subqueries: req_hrs, e_hrs, inprog_hrs. I need to add together the e_hrs and inprog_hrs, thus seeing whether that number is greater than or equal to req_hrs. If that is true, I need to return either an * or a null value.
Can someone please explain to me how I can add the two subquery results (e_hrs and inprog_hrs) together, and then compare that result to req_hrs thus returning said * or NVL? Code is below, Thank you:
SELECT
spriden_last_name lname,
spriden_first_name fname,
spriden_mi mi,
spriden_id id,
x.shrdgmr_majr_code_1 majr,
x.shrdgmr_grad_date grad_dt,
x.shrdgmr_degs_code degs,
DECODE(stvdegs_award_status_ind,'A','*',NULL) award_ind,
**DECODE(NVL(m.smbagen_req_credits_overall,0),0,
DECODE(NVL(sorcmjr_req_hours_ssdf,0),0,
DECODE(stvdegc_acat_code,'22',32,'23',64,'24',124,'42',42,999),
sorcmjr_req_hours_ssdf),m.smbagen_req_credits_overall) req_hrs,**
**TRUNC(shrlgpa_hours_earned,2) AS e_hrs,**
**(SELECT
NVL(SUM(sfrstcr_credit_hr),0)
FROM
sfrstcr
WHERE
sfrstcr_term_code = '&inprog_term'
AND sfrstcr_pidm = x.shrdgmr_pidm
AND sfrstcr_rsts_code IN ('RE','RW')
AND NOT EXISTS (
SELECT
'Y'
FROM
shrtckn,
shrtckg j
WHERE
shrtckn_pidm = sfrstcr_pidm
AND shrtckn_term_code = sfrstcr_term_code
AND shrtckn_crn = sfrstcr_crn
AND j.shrtckg_pidm = shrtckn_pidm
AND j.shrtckg_term_code = shrtckn_term_code
AND j.shrtckg_tckn_seq_no = shrtckn_seq_no
AND j.shrtckg_seq_no = (
SELECT
MAX(k.shrtckg_seq_no)
FROM
shrtckg k
WHERE
k.shrtckg_pidm = shrtckn_pidm
AND k.shrtckg_term_code = shrtckn_term_code
AND k.shrtckg_tckn_seq_no = shrtckn_seq_no))) AS inprog_hrs,**
ROUND(shrlgpa_gpa,2) gpa,
DECODE(SIGN(shrlgpa_gpa - 3.90),0,'S',1,'S',
DECODE(SIGN(shrlgpa_gpa - 3.75),0,'M',1,'M',
DECODE(SIGN(shrlgpa_gpa - 3.50),0,'C',1,'C',NULL))) latin,
(SELECT
m.shrasdl_astd_code_dl
FROM
shrasdl m
WHERE
m.shrasdl_term_code_effective = (
SELECT
MAX(n.shrasdl_term_code_effective)
FROM
shrasdl n)
AND m.shrasdl_min_gpa_term = (
SELECT
MAX(n.shrasdl_min_gpa_term)
FROM
shrasdl n
WHERE
n.shrasdl_term_code_effective = m.shrasdl_term_code_effective
AND shrlgpa_gpa >= n.shrasdl_min_gpa_term)) honors
FROM
shrdgmr x,
stvdegs,
stvdegc,
spriden,
sorcmjr,
smbagen m,
shrlgpa
WHERE
TO_CHAR(x.shrdgmr_grad_date,'MON-YY') IN ('&grad_dt1', NVL('&grad_dt2','XXX-
00'))
AND x.shrdgmr_seq_no = (
SELECT
MAX(z.shrdgmr_seq_no)
FROM
shrdgmr z
WHERE
z.shrdgmr_pidm = x.shrdgmr_pidm
AND z.shrdgmr_majr_code_1 = x.shrdgmr_majr_code_1
AND z.shrdgmr_grad_date IS NOT NULL)
AND stvdegs_code = x.shrdgmr_degs_code
AND stvdegc_code = x.shrdgmr_degc_code
AND spriden_pidm = x.shrdgmr_pidm
AND spriden_change_ind IS NULL
AND sorcmjr_cmjr_rule(+) = x.shrdgmr_cmjr_rule_1_1
AND REPLACE(m.smbagen_area(+),'-CORE','') = x.shrdgmr_majr_code_1
AND m.smbagen_active_ind(+) = 'Y'
AND m.smbagen_term_code_eff(+) <= x.shrdgmr_term_code_grad
AND ((m.smbagen_area IS NULL)
OR (m.smbagen_area IS NOT NULL
AND m.smbagen_term_code_eff = (
SELECT
MAX(n.smbagen_term_code_eff)
FROM
smbagen n
WHERE
REPLACE(n.smbagen_area,'-CORE','') = x.shrdgmr_majr_code_1
AND n.smbagen_active_ind = 'Y'
AND n.smbagen_term_code_eff <= x.shrdgmr_term_code_grad)))
AND shrlgpa_pidm(+) = x.shrdgmr_pidm
AND shrlgpa_levl_code(+) = x.shrdgmr_levl_code
AND shrlgpa_gpa_type_ind(+) = 'O'
ORDER BY
spriden_last_name,
spriden_first_name,
spriden_mi
;
Compare the sum of the union of the two sources with the required amount.
In simplified form:
select somekey, sum(hrs) worked_hrs, sum(req_hrs) required_hrs
from (
select
somekey,
e_hrs hrs
from e_hrs_table
where ...
union all -- the "all" is important to leave in!
select
somekey,
inprog_hrs
from inprog_hrs_table
where ...
) x
join req_hrs_table on req_hrs_table.somekey = x.somekey
where ... -- add req_hrs_table conditions here
group by somekey
You can add
having sum(hrs) < sum(req_hrs)
if you want only those rows that did not meet the quota.
I'm looking for a way to order my results based on the actual time. In my table yo can see values like:
1,23:45
2,9:45
3,27:43
When I do a query I would like to know how to order them based on their actual 24 hour time.
Ex:
3,3:43
2,9:45
1,23:45
Notice how it changes 27:43 to 3:43, and creates the order.
Where I am using it, in this query:
SELECT *,COALESCE(ADDTIME(s.`departure_time`,SEC_TO_TIME(rt.delay)),s.`departure_time`) as `rt_time` FROM `stop_times` s INNER JOIN `trips` t ON s.`trip_id` = t.`trip_id` INNER JOIN `stops` st ON st.`stop_id` = s.`stop_id` INNER JOIN `routes` r ON r.`route_id` = t.`route_id` LEFT JOIN `rt_trips` rt ON t.`trip_id` = rt.`trip_id` where (s.`stop_id` = 'CB900') and ( ( s.`departure_time` >= '00:50' and s.`departure_time` <= '05:50') OR ( s.`departure_time` >= '24:50' and s.`departure_time` <= '29:50') ) and (s.`pickup_type` = '0') and (t.`service_id` IN ('removed to make it easier')) HAVING (`rt_time` BETWEEN '01:50' and '05:50' ) ) OR ( `rt_time` BETWEEN '25:50' and '29:50' ) ORDER BY `order` ASC
Explanation:
Information is a transit schedule, that may go forward onto the next day which may be a saturday. So, times may become 25:50, where that means 1:50 the next day.
Thanks
Cyrus
Hmmm, if you just want to get a value between 0 and 24 hours, then I would do:
select concat(mod(substring_index(time_column, ':', 1) + 0, 24), ':',
substring_index(time_column, ':', -1)
)
Try this function on the time_column
concat(mod(substr(time_column,1,INSTR(time_column, ':')-1),24)
,substr(time_column,INSTR(time_column, ':'),3)
)
You might need to cast date to string to integer, do the maths, and again cast it to time. But the fiddle version seems to work properly on varchar to integer conversion. Check this
http://sqlfiddle.com/#!9/ff60f9/1
I have a table that contains events data. I also have a list of dates that I need to check through. So I loop though the given dates and query my table. I need to select the events that we on a given date and if the date is the same as today, I only need events that took place before certain time. The time is stored in a military format, like 14:30.
I think I've overcomplicated my query logic:
$current_date = date('Y-m-d');
$current_time = date('H:i');
The reason I use PHP time and date, because I can have an accurate time from a server I control. MySQL time is off by a few hours...
SELECT *
FROM events
WHERE org.id = ".$ID."
AND (
(
DATE(events.date) = '".$givenDate."'
AND
'".$givenDate."' < '".$current_date."'
)
OR
(
DATE(events.date) = '".$givenDate."'
AND
'".$givenDate."' < '".$current_date."'
AND
events.time < '".$current_time."'
)
)
SELECT *
FROM events
WHERE org.id = ".$ID."
AND date(events.date) = '".$givenDate."'
and if ('".$givenDate."' = '".$current_date."',
events.time < '".$current_time."', true)
I'm trying to query $wpdb to get back an int value of the number of users in a custom table who have recorded a number of hours volunteer work above a set target - these hours need to have been moderated ( value set to = 1 ) - I have this so far:
EDIT - updated to use consistent {} around php variables in query --
$target = get_post_meta($post->ID, 'target', true) ? (int)get_post_meta($post->ID, 'target', true) : 100;
$awards = $wpdb->get_var("
SELECT user_id
FROM {$this->options['rewards_logging']}
WHERE moderated = 1 AND reward_id = {$post->ID}
GROUP BY user_id
HAVING sum(hours) > {$target}
");
Which returns the correct value of '0' if none of the hours are approved ( moderated = 0 ), however as soon as one of those users hours are approved, this query returns the count of all the users who have logged more than the target hours ( whether they have been approved or not ).
Any pointers!
Cheers
Ray
Seems I was trying to get back a single variable using $wpdb->get_var, when I really needed the whole result set:
$awards = $wpdb->get_results("
SELECT user_id
FROM {$this->options['rewards_logging']}
WHERE moderated = 1 AND reward_id = {$post->ID}
GROUP BY user_id
HAVING sum(hours) > {$target}
");
Then I can check over the data and display a result - etc...:
if ( count($awards) > 0 ) {
#var_dump($awards);
echo '<span class="awards-notice">'.count($awards).'</span>';
} else {
echo '-';
}