如果日期在当前月份之前,则将日期转换为当月的第一个月

时间:2021-06-25 15:37:22

I have a query with an IIF() expression in one column that I am using to identify if a date is previous to the current month to then amend it if so. So if I run the query on 19th March 2014 and the EffFrom date is before 1st March 2014, I would want that column entry to now appear as 1st March 2014.

我在一列中有一个IIF()表达式的查询,我用它来识别日期是否在当前月份之前,然后再修改它。因此,如果我在2014年3月19日运行查询并且EffFrom日期在2014年3月1日之前,我希望该列条目现在显示为2014年3月1日。

I am using the below expression which is pretty much doing what I want, however I know it is not considering the year -- i.e. it is changing an entry of 1st Jan 2015 to be 1st March 2014.

我正在使用下面的表达式,它几乎可以满足我的需求,但我知道它不考虑今年 - 即它将2015年1月1日的条目更改为2014年3月1日。

EffFrom: 
IIf(Month([Table.Efffrom])"Less than symbol"Month(Date()),Date()-Day(Date())+1,[Table.Efffrom])

Can someone correct the expression for me?

有人可以为我纠正这个表达吗?

2 个解决方案

#1


3  

I interpreted "if I run the query on 19th March 2014 and the efffrom date is before 1st March 2014 I would want that column entry to now appear as 1st March 2014" to mean you want something like this from a query run today (Mar 19th 2014):

我解释说“如果我在2014年3月19日运行查询,并且生效日期是在2014年3月1日之前,我希望该列条目现在显示为2014年3月1日”,这意味着您今天要查询此类内容(3月19日) 2014):

id Efffrom   adjusted_date
 1  1/1/2014      3/1/2014
 2  3/1/2014      3/1/2014
 3 3/31/2014     3/31/2014
 4  1/1/2015      1/1/2015

If that is correct, your IIf expression can use DateSerial to check whether Efffrom is before the first of this month, and transform the older dates.

如果这是正确的,您的IIf表达式可以使用DateSerial检查Efffrom是否在本月的第一天之前,并转换旧日期。

SELECT
    y.id,
    y.Efffrom,
    IIf
        (
            y.Efffrom < DateSerial(Year(Date()), Month(Date()), 1),
            DateSerial(Year(Date()), Month(Date()), 1),
            y.Efffrom
        ) AS adjusted_date
FROM YourTable AS y;

#2


0  

Here's what i do to get the first day of the next month

这是我为了下个月的第一天所做的工作

EffFrom: DateAdd("m",1,CDate(Format([Table.Efffrom],"m/\1/yy")))

#1


3  

I interpreted "if I run the query on 19th March 2014 and the efffrom date is before 1st March 2014 I would want that column entry to now appear as 1st March 2014" to mean you want something like this from a query run today (Mar 19th 2014):

我解释说“如果我在2014年3月19日运行查询,并且生效日期是在2014年3月1日之前,我希望该列条目现在显示为2014年3月1日”,这意味着您今天要查询此类内容(3月19日) 2014):

id Efffrom   adjusted_date
 1  1/1/2014      3/1/2014
 2  3/1/2014      3/1/2014
 3 3/31/2014     3/31/2014
 4  1/1/2015      1/1/2015

If that is correct, your IIf expression can use DateSerial to check whether Efffrom is before the first of this month, and transform the older dates.

如果这是正确的,您的IIf表达式可以使用DateSerial检查Efffrom是否在本月的第一天之前,并转换旧日期。

SELECT
    y.id,
    y.Efffrom,
    IIf
        (
            y.Efffrom < DateSerial(Year(Date()), Month(Date()), 1),
            DateSerial(Year(Date()), Month(Date()), 1),
            y.Efffrom
        ) AS adjusted_date
FROM YourTable AS y;

#2


0  

Here's what i do to get the first day of the next month

这是我为了下个月的第一天所做的工作

EffFrom: DateAdd("m",1,CDate(Format([Table.Efffrom],"m/\1/yy")))