简体   繁体   中英

How to filter the data based on particualr column for the current fiscal year in sql

Fiscal Duration is: July to June I have a [Due Date] column in the table. Now i need to filter the remaining columns data based on [Due Date] column which is Current Fiscal Year Data. Here Requirement is i should not do hardcode for getting the date, because if we entered in to next fiscal year, we have to get next fiscal year data automatically.

Please can any one can suggest either the DAX measure or Sql Query for this logic.

  1. Extract the year and month from your table by creating a view (ViewStep1) that uses those attributes:

    TO_NUMBER (TO_CHAR ((DUE_DATE), 'YYYY')) AS YEAR

    TO_NUMBER (TO_CHAR ((DUE_DATE), 'MM')) AS MONTH

  2. Then you can use those columns to filter for each fisical year. Or even better, create another view that has a column FISICAL_YEAR which gets it´s data from a subselect of your previous view. Add a simple case in that subselect and your done:

    select YEAR as YEAR, MONTH as MONTH, DUE_DATE, (case when MONTH >= 7 then YEAR else (YEAR - 1) END) as FISICAL_YEAR from ViewStep1 order by YEAR desc, MONTH desc, DUE_DATE desc;

If you have issues creating a view then I would advise to look into the documentation of your database system.

To get the fiscal year, just subtract six months. So the current fiscal year would be something like this:

where year(dateadd(month, -6, duedate)) = year(dateadd(month, -6, duedate))

You should be able to adapt this idea to your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM