How to calculate YTD and Last year YTD on Date in Bex

Hi,
I have a requirement in which I need to show the report as following format.
I need to show    Year>monthday as Hierarchy. user will drill down on Month to Date.
             Del Qty              Ord Qty              Del  Qty LY              Del Qty YTD                  Del Qty  L YTD
Year
> Drill Down to Months
        Jan
        Feb
        Mar
          ---> 1.03.2010
          ---> 2.03.2010
          ---> 3.03.2010
        Dec
I have Calday, Calmonth, Calyear all three objects.
Now my question is how I can achive this? On which i need to create hierarchy?
and how to calculate the YTD at day level.
Note:- in the selection calyear is mandatory. User wants to display as per year.   So What infoobject I need to take in selection?
and the way to calculate YTD on Day (in the case also ,where user doesn't give the date in selection)
Plz reply asap its uregent !!!
Thanks in advance,
Amit

Hi,
As per my understanding I think YTD means JAN to current date result. I means say if user enter 062005 then result in YTD should be from 012005 to 062005.
And to do this you need to write a customer exit variable where LOW field of internal table you will put JAN year and HIGH will contain month year entered by user.
Hope this will help you.
Suneel

Similar Messages

  • YTD , Last year YTD help needed in OBIEE 11g

    Hi all,
    I have a report which looks this way
    Banner Name
    *Sales YTD*
    *Sales Last year YTD*
    YTD values are based on actual date of running report.
    *so ex: the report is ran on 10-feb-2013, the Sales YTD will be from
    01-Jan-2013 till 10-Feb-2013.*
    * Sales Last year YTD will be 01-Jan-2012 till 10-feb-2012.*
    I have achieved Sales YTD with my Time dimension.
    My time dimension has the data for Last year , year ago date!
    Can you please help me out on how to proceed for the Last year YTD
    calculation?
    Its helpful to share any syntax..
    Regards
    Kumar

    Hi,,
    you can use Todate Function.
    Take a Look @ this http://mkashu.blogspot.in/2013/08/obiee-model-time-series-in-obiee.html
    Regards,
    VG

  • How to design for YTD, Monthly & Last year Key figures

    Hi Gurus:
    Can you pls tell me how to design a data model, where a user wants to get YTD, Monthly & Last year Key figures say Sales volume...
    Is aggregates the answer?
    Pls let me know...Thanks a lot

    Hi Vijay,
    I am a beginner in BI field.
    I have a problem till now i couldnt get the right answer.
    That the reason why i write you today.
    My Problem is with a Query.
    I'm designed a dynamic query which should look like this:
    12.2005-- Jan06 --- Feb06 --- Mar06 --- Apr06 ... Dec06 --- YTD
    My Problem, if you give per e.g Dezember, all Months have to appear. Or you give may, your going to receive till may.The result i receive till now its only the month i gave.How can i get the correct answer? How i am going to calculate the YTD?
    You can directly answer hier:[email protected]
    Thanks

  • BI Query Monthly (this year and last year) vs YTD (this year and last year)

    I have a query requirement in BI to create a Query which will give me
    Sales Monthly (this year and last)  vs YTD Sales (this year and last).
    Here is an example of how the query would look:
    01/2007     01/2008     02/2007     02/2008     03/2007     03/2008     YTD 2007 YTD 2008
    Above would represent CKF's by month and YTD with Text Variables.
    Is this possible?  Obviously my problem is the number of columns that I would need, since I would not always be reporting on the same month(s).  Any suggestions (if this is possible).
    Thanks

    Hi,
    You can do it, see the following code and change as per your requirement...
    FMYEAR and LMYEAR Customer Exit variables on 0CALMONTH.  If you have any input variable then link it.
    Note:  Use Offsets for the following Variables to get 2007,2008,2009 results...
    * First Month Of Current Fiscal year comment*
        WHEN 'FMYEAR'.
          CALL FUNCTION 'DATE_TO_PERIOD_CONVERT'
                 EXPORTING
                   i_date               = sy-datum
    *         I_MONMIT             = 00
                   i_periv              = 'V3'
                IMPORTING
                  e_buper              =  zbuper
                  e_gjahr              =  zbdatj.
          CLEAR: l_s_range.
          l_s_range-low+4(2) = '04'.
          l_s_range-low+0(4) = zbdatj.
          l_s_range-sign = 'I'.
          l_s_range-opt = 'EQ'.
          APPEND l_s_range TO e_t_range.
    * Last Month of Current Fiscal year*
        WHEN 'LMYEAR'.
          CALL FUNCTION 'DATE_TO_PERIOD_CONVERT'
                 EXPORTING
                   i_date               = sy-datum
    *         I_MONMIT             = 00
                   i_periv              = 'V3'
                IMPORTING
                  e_buper              =  zbuper
                  e_gjahr              =  zbdatj.
          CLEAR: l_s_range.
          l_s_range-low+4(2) = '03'.
          l_s_range-low+0(4) = zbdatj + 1.
          l_s_range-sign = 'I'.
          l_s_range-opt = 'EQ'.
          APPEND l_s_range TO e_t_range.
    Thanks
    Reddy

  • How to show current year and last year sales in a WEBI Report

    Hi Guys
    How can show current YEar Sales in one column and Last YEar Sales in the other column based on a user prompt for the Current YEar Column.
    For Example is user enter 2010 for Year how can i show a Column for Sales-2010 and Sales 2009.
    Thanks

    If you can modify your Universe add an object named New Object Last Year whose SQL is:( yourTableName.Year + 1)
    Then in WebI create two distinct queries in your query Pane. In the first one you could do this:
    Query 1:
    objects: Year, Sales ... etc.
    filters:   Year Equal to '1. Prompt Year'
    Query 2:
    objects: Year, Sales ... etc.
    filters:    New Object Last Year Equal to '1.Prompt Year'
    Then in your report you can drag each object on their respective columns.
    If you don't want to use two distinct queries, use one like this:
    Query 1:
    objects: Year, Sales ... etc.
    filters:        Year Equal to '1. Prompt Year'
                 Or
                      New Object Last Year Equal to '1.Prompt Year'
    Edited by: PadawanGirl on Jun 23, 2011 6:28 PM

  • How you will see reports last year and till date reports and compare?

    How you will see reports last year and till date reports and how you will compare?
    sridhar

    Hi.
        This you can achieve at query level.
    I have few question.. First let me know how many KFs you have in the query and based on what characteristic you want to compare the KFs.
    Let me assume that you want to compare "SALES" KF for the last and current year.
    1) Add the "SALES" KF to the Query.
    2) Rename it as "SALES - Prior Year"
    3) Copy the above KF and rename the new KF as "SALES - Current Year"
    4) In the "SALES - Prior Year", add the date (say, Calendar Date, Fiscal Period, etc.) as per yr requirement. Keep offset as "-1"
    5) Do the same for "SALES - Current Year" . No offset.
    6) Now create a new formula to find the difference between "SALES - Prior Year"  and "SALES - Current Year" .
    Hope you got some idea!!
    Let me know if anything more is required.
    Regards,
    Balaji V

  • Help with report that shows Year and Last Year

    Hello!
    I want to view in a report some KPIs (Rows) and Years(Columns) but I want a specific report with a prompt, I mind, at top of my dashboard there is a Year prompt that it contains (2001, 2002, 2003….,2010) and I want to see in a report when I chose in prompt 2008 for example that my report shows me 2008 Year and Last Year (2007) and another column that indicate Diference between these years. Is that possible? Can u explain me the process to get it?
    Thank you very much!

    If you get access, create a duplicate of the Measure you wish to use in the Business Model.
    Once you have a duplicate rename to say $ Revenue Year Ago and then from the properties of the Measure, General click on Use existing logical columns as the source.
    Then select function>Time Series> Ago
    Ago has 3 inputs, select the column you want to base the measure on, the TIME SERIES Dimensions for year, and then 1 for 1 year ago.
    So your formula will look something like:
    Ago("Human Resources"."Fact - Absence"."# Days" , "Human Resources"."Time"."Year" , 1)
    Save this and copy into your Presentation layer available for use.
    Hope this helps

  • How to calculates averages and send to a file?(source code)

    how to calculates averages and send to a file?(source code) I need to get started but I don't know where to begin...........the book I have doesn't really say how to handle averages and send to a file.................anybody got suggestions on how to get started? I am a newbie so place in layman's terms.............thanx

    To calculate an average: add all of the numbers together and divide by how many numbers you added together.
    ex: (1 + 2 + 3 + 10)/4 = 4 (4 is the average)
    As for writing them to a file, look over the API for random or sequential access files.

  • How to calculate excise and vat

    can anybody tell me how to calculate excise and vat as i am doing a alv reports from me23n and i have to put excise and vat in respect to document invoice number in me23n.i am using tables mseg,makt,bseg,ekpo so if there any related program please refer me.

    In this  report i have to calculate excise and vat according to po number and also i dont how to put so plesae solve this if possible......
    *& Report  ZFA_GRS
    *&RD1K903618
    REPORT  ZFA_GRS.
    TABLES : ekpo,EKKO, t001,MSEG,MKPF,komk,komp.
    TYPE-POOLS : SLIS.
    DATA : IT_FCAT TYPE SLIS_T_FIELDCAT_ALV,
           WA_FCAT TYPE SLIS_FIELDCAT_ALV.
    DATA : IT_SORT TYPE SLIS_T_SORTINFO_ALV,
           WA_SORT TYPE SLIS_SORTINFO_ALV.
    DATA:  GT_TOP_OF_PAGE TYPE SLIS_T_LISTHEADER,
           LS_LINE TYPE SLIS_LISTHEADER.
    DATA : BEGIN OF IT_FINAL OCCURS 0,
           LIFNR LIKE MSEG-LIFNR,
           MATNR LIKE MSEG-MATNR,
           MBLNR LIKE MSEG-MBLNR,
           BELNR LIKE BSEG-BELNR,
           MAKTX LIKE MAKT-MAKTX,
           MENGE LIKE MSEG-MENGE,
           MEINS LIKE MSEG-MEINS,
           BWART LIKE MSEG-BWART,
           EBELN like MSEG-EBELN,
           EBELP LIKE MSEG-EBELP,
           DMBTR like BSEG-DMBTR ,
           EXAMT TYPE DMBTR,
           WRBTR LIKE BSEG-WRBTR,
          EXAMT LIKE BSEG-WRBTR,
           HKONT LIKE BSEG-HKONT,
           NETVALUE type DMBTR,
           VATAMT TYPE DMBTR,
           TOTAMT TYPE DMBTR,
           MWSBP LIKE KOMP-MWSBP,
           EXBED LIKE J_1IEXCDTL-EXBED,
           EXSED LIKE J_1IEXCDTL-EXSED,
           EXAED LIKE J_1IEXCDTL-EXAED,
           CESS LIKE J_1IEXCDTL-CESS,
           ECS LIKE J_1IEXCDTL-ECS,
           EXADDTAX1 LIKE J_1IEXCDTL-EXADDTAX1,
           VAT TYPE KONV-KWERT,
           UNIT TYPE EKPO-MEINS,
           TOTQNT TYPE MSEG-MENGE,
           NETWR TYPE EKPO-NETWR,
           PEINH LIKE EKPO-PEINH,
           NETPR LIKE EKPO-NETPR,
           menge1 like ekpo-menge,
           TOTEXC TYPE DMBTR,
           TOTVAT TYPE DMBTR,
           TOTVAL TYPE DMBTR,
    END OF IT_FINAL.
    DATA : WA_FINAL LIKE IT_FINAL OCCURS 0 WITH HEADER LINE,
           W_J_1IEXCDTL LIKE STANDARD TABLE OF J_1IEXCDTL WITH HEADER LINE.
    DATA : WA_FINAL LIKE IT_FINAL OCCURS 0 WITH HEADER LINE.
    SELECTION-SCREEN: BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
    SELECTION-SCREEN SKIP 1.
    SELECT-OPTIONS: P_MBLNR FOR MSEG-MBLNR .
    SELECTION-SCREEN SKIP 1.
    SELECTION-SCREEN : END OF BLOCK B1.
    SELECT MATNR MBLNR MENGE MEINS BWART EBELN EBELP DMBTR FROM MSEG INTO CORRESPONDING FIELDS OF TABLE IT_FINAL WHERE MBLNR IN P_MBLNR AND
                ( BWART = '101'  OR  BWART = '102' OR
                  BWART = '113'  OR  BWART = '114'  )  .
    DATA : TOTAMT TYPE DMBTR,
           NETVAL TYPE DMBTR,
           NETVALUE TYPE DMBTR,
           NETWR LIKE EKPO-NETWR,
           EXAMT TYPE DMBTR.
    SORT IT_FINAL BY MBLNR.
    LOOP AT IT_FINAL.
    ENDLOOP.
    LOOP AT IT_FINAL INTO WA_FINAL.
    SELECT SINGLE MAKTX FROM MAKT INTO WA_FINAL-MAKTX WHERE MATNR = WA_FINAL-MATNR.
    SELECT single  DMBTR from BSEG into  wa_final-DMBTR where EBELN = wa_final-EBELN .
    SELECT SINGLE WRBTR FROM BSEG INTO WA_FINAL-WRBTR WHERE EBELN = WA_FINAL-EBELN.
    *WA_FINAL-WRBTR = W_J_1IEXCDTL-EXBED + W_J_1IEXCDTL-EXSED + W_J_1IEXCDTL-EXAED + W_J_1IEXCDTL-CESS + W_J_1IEXCDTL-ECS + W_J_1IEXCDTL-EXADDTAX1.
    *wa_final-EXAMT = Wa_final-EXBED + wa_final-EXSED + Wa_final-EXAED + Wa_final-CESS + Wa_final-ECS + Wa_final-EXADDTAX1.
    SELECT SINGLE BELNR FROM BSEG INTO WA_FINAL-BELNR WHERE EBELN = WA_FINAL-EBELN.
    MODIFY IT_FINAL FROM WA_FINAL TRANSPORTING   BELNR
                                                 MAKTX
                                                 MENGE
                                                 MEINS
                                                 BWART
                                                 EBELN
                                                 EBELP
                                                 DMBTR
                                                 WRBTR
                                                 HKONT
                                                 LIFNR
                                                NETVAL
                                                NETVALUE
                                                 EXAMT
                                                 VATAMT
                                                 TOTAMT
                                                 MWSBP
                                                 VAT
                                                 UNIT
                                                 TOTQNT
                                                TOTNVL
                                                 TOTEXC
                                                 TOTVAT
                                                 TOTVAL
                                                 PEINH
                                                 netwr
                                                 netpr
    ENDLOOP.
    PERFORM GEN_TOP.
    *TOTAMT = NETVAL.
    WA_FCAT-COL_POS = 1.
    WA_FCAT-FIELDNAME = 'MATNR'.
    WA_FCAT-SELTEXT_M = 'MATERIAL NO.'.
    WA_FCAT-TABNAME = 'IT_FINAL'.
    WA_FCAT-OUTPUTLEN = 18.
    APPEND WA_FCAT TO IT_FCAT.
    WA_FCAT-COL_POS = 2.
    WA_FCAT-FIELDNAME = 'MBLNR'.
    WA_FCAT-SELTEXT_M = 'MAT.DOC NO'.
    WA_FCAT-TABNAME = 'IT_FINAL'.
    WA_FCAT-OUTPUTLEN = 15.
    APPEND WA_FCAT TO IT_FCAT.
    WA_FCAT-COL_POS = 3.
    WA_FCAT-FIELDNAME = 'BELNR'.
    WA_FCAT-SELTEXT_M = 'INVOICE DOC NO'.
    WA_FCAT-TABNAME = 'IT_FINAL'.
    WA_FCAT-OUTPUTLEN = 15.
    APPEND WA_FCAT TO IT_FCAT.
    WA_FCAT-COL_POS = 4.
    WA_FCAT-FIELDNAME = 'EBELN'.
    WA_FCAT-SELTEXT_M = 'PO DOC NO.'.
    WA_FCAT-TABNAME = 'IT_FINAL'.
    WA_FCAT-OUTPUTLEN = 12.
    APPEND WA_FCAT TO IT_FCAT.
    WA_FCAT-COL_POS = 5.
    WA_FCAT-FIELDNAME = 'MAKTX'.
    WA_FCAT-SELTEXT_M = 'DESCRIPTION'.
    WA_FCAT-TABNAME = 'IT_FINAL'.
    WA_FCAT-OUTPUTLEN = 35.
    APPEND WA_FCAT TO IT_FCAT.
    WA_FCAT-COL_POS = 6.
    WA_FCAT-FIELDNAME = 'MENGE'.
    WA_FCAT-SELTEXT_M = 'QUANTITY'.
    WA_FCAT-TABNAME = 'IT_FINAL'.
    WA_FCAT-OUTPUTLEN = 8.
    APPEND WA_FCAT TO IT_FCAT.
    WA_FCAT-COL_POS = 7.
    WA_FCAT-FIELDNAME = 'MEINS'.
    WA_FCAT-SELTEXT_M = 'UNIT'.
    WA_FCAT-TABNAME = 'IT_FINAL'.
    WA_FCAT-OUTPUTLEN = 5.
    APPEND WA_FCAT TO IT_FCAT.
    WA_FCAT-COL_POS = 8.
    WA_FCAT-FIELDNAME = 'BWART'.
    WA_FCAT-SELTEXT_M = 'MOVTYPE.'.
    WA_FCAT-TABNAME = 'IT_FINAL'.
    WA_FCAT-OUTPUTLEN = 8.
    APPEND WA_FCAT TO IT_FCAT.
    WA_FCAT-COL_POS = 9.
    WA_FCAT-FIELDNAME = 'DMBTR'.
    WA_FCAT-SELTEXT_M = 'NETVALUE'.
    WA_FCAT-TABNAME = 'IT_FINAL'.
    WA_FCAT-OUTPUTLEN = 12.
    APPEND WA_FCAT TO IT_FCAT.
    *WA_FCAT-COL_POS = 9.
    *WA_FCAT-FIELDNAME = 'EBELP'.
    *WA_FCAT-SELTEXT_M = 'LINE ITEM.'.
    *WA_FCAT-TABNAME = 'IT_FINAL'.
    *WA_FCAT-OUTPUTLEN = 12.
    *APPEND WA_FCAT TO IT_FCAT.
    WA_FCAT-COL_POS = 10.
    WA_FCAT-FIELDNAME = 'WRBTR'.
    WA_FCAT-SELTEXT_M = 'EXCISE'.
    WA_FCAT-TABNAME = 'IT_FINAL'.
    WA_FCAT-DO_SUM = 'X'.
    WA_FCAT-OUTPUTLEN = 15.
    APPEND WA_FCAT TO IT_FCAT.
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      I_INTERFACE_CHECK                 = ' '
      I_BYPASSING_BUFFER                = ' '
      I_BUFFER_ACTIVE                   = ' '
       I_CALLBACK_PROGRAM                = SY-CPROG
      I_CALLBACK_PF_STATUS_SET          = ' '
      I_CALLBACK_USER_COMMAND           = ' '
       I_CALLBACK_TOP_OF_PAGE            = 'ALV_TOP_OF_PAGE'
      I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
      I_CALLBACK_HTML_END_OF_LIST       = ' '
      I_STRUCTURE_NAME                  =
      I_BACKGROUND_ID                   = ' '
      I_GRID_TITLE                      =
      I_GRID_SETTINGS                   =
      IS_LAYOUT                         =
       IT_FIELDCAT                       = IT_FCAT
      IT_EXCLUDING                      =
      IT_SPECIAL_GROUPS                 =
      IT_SORT                           =
      IT_FILTER                         =
      IS_SEL_HIDE                       =
       I_DEFAULT                         = 'X'
       I_SAVE                            = 'A'
      IS_VARIANT                        =
      IT_EVENTS                         =
      IT_EVENT_EXIT                     =
      IS_PRINT                          =
      IS_REPREP_ID                      =
      I_SCREEN_START_COLUMN             = 0
      I_SCREEN_START_LINE               = 0
      I_SCREEN_END_COLUMN               = 0
      I_SCREEN_END_LINE                 = 0
      I_HTML_HEIGHT_TOP                 = 0
      I_HTML_HEIGHT_END                 = 0
      IT_ALV_GRAPHICS                   =
      IT_HYPERLINK                      =
      IT_ADD_FIELDCAT                   =
      IT_EXCEPT_QINFO                   =
      IR_SALV_FULLSCREEN_ADAPTER        =
    IMPORTING
      E_EXIT_CAUSED_BY_CALLER           =
      ES_EXIT_CAUSED_BY_USER            =
      TABLES
        T_OUTTAB                          = IT_FINAL
    EXCEPTIONS
      PROGRAM_ERROR                     = 1
      OTHERS                            = 2
    IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    FORM ALV_TOP_OF_PAGE.
      CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
        EXPORTING
          I_LOGO             = 'ZROHALOGO'
          IT_LIST_COMMENTARY = GT_TOP_OF_PAGE.
    ENDFORM.                    "alv_top_of_page
    *&      Form  GEN_TOP
          text
    -->  p1        text
    <--  p2        text
    FORM GEN_TOP .
    DATA: INFO(60),
            B_DT(10), E_DT(10).
      REFRESH : GT_TOP_OF_PAGE.
      CLEAR : LS_LINE.
      LS_LINE-TYP = 'H'.
      LS_LINE-INFO = '       ROHA DYECHEM PVT LTD'.
      APPEND LS_LINE TO GT_TOP_OF_PAGE.
      CLEAR : LS_LINE.
      LS_LINE-TYP = 'S'.
    LS_LINE-KEY = 'Address'.
    LS_LINE-INFO =
      APPEND LS_LINE TO GT_TOP_OF_PAGE.
      CLEAR : LS_LINE.
      LS_LINE-TYP = 'S'.
    LS_LINE-INFO =
      APPEND LS_LINE TO GT_TOP_OF_PAGE.
      CLEAR : LS_LINE.
      LS_LINE-TYP = 'S'.
    LS_LINE-INFO =
      APPEND LS_LINE TO GT_TOP_OF_PAGE.
      CLEAR : LS_LINE.
      CLEAR LS_LINE.
      LS_LINE-TYP = 'S'.
      LS_LINE-KEY = 'Report Name'.
      LS_LINE-INFO = 'NEW GRS REPORT'.
      APPEND LS_LINE TO GT_TOP_OF_PAGE.
      CLEAR LS_LINE.
      LS_LINE-TYP = 'S'.
      LS_LINE-KEY = 'T-CODE'.
    LS_LINE-INFO =
      APPEND LS_LINE TO GT_TOP_OF_PAGE.
      CLEAR LS_LINE.
      LS_LINE-TYP = 'S'.
    LS_LINE-KEY = 'Period'.
      LS_LINE-INFO = INFO.
      APPEND LS_LINE TO GT_TOP_OF_PAGE.
      WRITE SY-DATUM TO B_DT.
      CLEAR LS_LINE.
      LS_LINE-TYP = 'S'.
      LS_LINE-KEY = 'Report Dt.'.
      LS_LINE-INFO = B_DT.
      APPEND LS_LINE TO GT_TOP_OF_PAGE.
    ENDFORM.                    " GEN_TOP

  • How to Plot number and string in one row (data logger counter) ?

    hi all i made data log quantity using Digital Counter via modbus to monitoring quantity and reject that has and Name Operator, Machine and Part Number.
    i have problem about plot the number & string in one row, as shown on the picture below :
    how to move that string on one row ? i attach my vi.
    Thanks~
    Attachments:
    MODBUS LIB Counter.vi ‏39 KB

    Duplicate and answered - http://forums.ni.com/t5/LabVIEW/How-to-Plot-number-and-string-in-one-row-data-logger-counter-via/m-p...

  • Finding Last friday and Last monday of current date

    Hi
    Please help me to get the Last friday and Last monday of current date using an sql query.
    If I am running query on 7th July 2008 ,it should give me Last friday as 07/04/2008 and Last monday as 07/07/2008
    I need to compare it with another date filed
    eg: Select * from employee where ReportedDate between [Last Friday] and [Last Monday]

    It works many times but fails for this scenario- If today is Friday and I run following query today, it throws today's date, which is incorrect. I need previous Friday's date to be returned everytime.
    select next_day (sysdate-7,'FRIDAY') Last_Friday from dual
    Edited by: user6402026 on May 22, 2013 1:43 PM
    Edited by: user6402026 on May 22, 2013 1:43 PM

  • How to calculate totals based on year (date) and comparing othe columns in the same table please

    Hello Good Evening,
    Could you please help me here
    how to write condition for self table year records, such 2012 name and acctno match with 2013 name and acctno then total, provided below,
    create table #tab1 (MasterKey int, AcctNo varchar(12),name varchar(25), SumaofShares numeric, request_dat datetime )
    --drop table #tab1
    insert into #tab1 values (1000, 100,'Tom', 2500, '10/01/2012')
    insert into #tab1 values (1001, 101,'Bat', 1550, '08/11/2012')
    insert into #tab1 values (1002, 102,'Kit', 1600, '06/12/2012')
    insert into #tab1 values (1003, 103,'Vat', 1750, '04/15/2012')
    insert into #tab1 values (1010, 104,'Sim',200, '04/21/2013')
    insert into #tab1 values (1011, 105,'Tim',500, '06/18/2013')
    insert into #tab1 values (1012, 100,'Tom',800, '08/22/2013')
    insert into #tab1 values (1013, 101,'Bat',550, '09/15/2013')
    insert into #tab1 values (1014, 100,'Pet',200, '02/21/2013')
    insert into #tab1 values (1015, 103,'Vat',150, '03/18/2013')
    insert into #tab1 values (1016, 110,'Sun',800, '03/22/2013')
    insert into #tab1 values (1017, 111,'Bet',550, '12/15/2013')
    insert into #tab1 values (9999, 111,'AAA',110, '12/15/2014')
    create table #tab2 (IssueKey int, totalOutstanding numeric, sharedBenefits varchar(1) )
    --drop table #tab2
    insert into #tab1 values (1000, 500, 'V')
    insert into #tab1 values (1001, 150, 'U')
    insert into #tab1 values (1002, 100, 'N')
    insert into #tab1 values (1003, 170, 'U')
    insert into #tab1 values (1010, 100, 'U')
    insert into #tab1 values (1011, 200, 'K')
    insert into #tab1 values (1012, 340, 'U')
    insert into #tab1 values (1013, 560, 'N')
    insert into #tab1 values (1014, 280, 'V')
    insert into #tab1 values (1015, 150, 'V')
    insert into #tab1 values (1016, 840, 'V')
    insert into #tab1 values (1017, 530, 'N')
    i would like to get 4 columns output
    how to get sumofshares (#tab1) and TotalOutStanding(#tab2) summ up with these values please.,
    MasterKey (#tab1) and IssueKey (#tab2) are like primary key and foreign key
    so the request is
    need to calculate, sumofshares (#tab1) and TotalOutStanding(#tab2) as below
    1)ShareBenefist = U and year( request_dat) in (2012 , 2103) and (Name for 2012 should match with 2013 name and 2012 Acctno should match with 2013 accounno) in (#tab1)
    then '2012 and 2013 accts UN Veriverted'
    2)ShareBenefist = V and year( request_dat) in (2012 , 2103) and (Name for 2012 should match with 2013 name and 2012 Acctno should match with 2013 accounno) in (#tab1)
    then '2012 and 2013 accts Veriverted'
    3)ShareBenefist = N and year( request_dat) in (2012 , 2103) and (Name for 2012 should match with 2013 name and 2012 Acctno should match with 2013 accounno) in (#tab1)
    then '2012 and 2013 accts NONVERT'
    4)year( request_dat) =2102 and Name and Acctno not match with 2013 account name and acctno (#tab1)
    then '2012 last year accounts'
    5)year( request_dat) = 2013 and Name and Acctno not match with 2013 account name and acctno (#tab1)
    then '2012 This year accounts'
    for ex 1) the below accounts in #tab1 has both 2012 and 2013 and acctno same in both years and name is same in both years so it is condired as
    insert into #tab1 values (1012, 100,'Tom',800, '08/22/2013')
    for ex 2)
    insert into #tab1 values (1013, 101,'Bat',550, '09/15/2013')
    for ex 4) 2012 records there is not match acctno and name in 2013 recods
    insert into #tab1 values (1002, 102,'Kit', 1600, '06/12/2012')
    for ex 5) 2013 records there is no match of name and acct no with 2012 records
    insert into #tab1 values (1010, 104,'Sim',200, '04/21/2013')
    insert into #tab1 values (1014, 100,'Pet',200, '02/21/2013')
    insert into #tab1 values (1016, 110,'Sun',800, '03/22/2013')
    insert into #tab1 values (1017, 111,'Bet',550, '12/15/2013')
    Expected Results (just for format)
    AcctTypeDescription, SumofShares, OtotalutStand
    '2012 and 2013 accts UN Veriverted',2700,234
    '2012 and 2013 accts Veriverted' ,2890,234
    '2012 and 2013 accts NONVERT' ,4533,325
    '2012 last year accounts' ,2334,567
    '2012 This year accounts' ,2222,877
    Please
    Thank youy in advance
    asita

    As I understand it, your sample output was only to show the format, and did not pretend to be give the exact result given the sample data. This is pity, because means that I was not able to verify that my query below gives the desired result.
    The exact relation between #tab1 and #tab2 is not clear to me. I'm making the assumption that a row in #tab1 may have zero or one row in #tab2, but not many.
    My solution has two CTEs. The first simply joins the tables together and extracts the year. In the second CTE, I perform a self-join over the first CTE which I have divided in two by year. This is a full join, since an account may appear for only one of
    the year. Note that it is instrumental to extract the year data in inner queries first.
    The final query is just an aggregation over the categories.
    create table #tab1 (MasterKey int, AcctNo varchar(12),name varchar(25), SumaofShares numeric, request_dat datetime )
    --drop table #tab1
    insert into #tab1 values (1000, 100,'Tom', 2500, '10/01/2012')
    insert into #tab1 values (1001, 101,'Bat', 1550, '08/11/2012')
    insert into #tab1 values (1002, 102,'Kit', 1600, '06/12/2012')
    insert into #tab1 values (1003, 103,'Vat', 1750, '04/15/2012')
    insert into #tab1 values (1010, 104,'Sim',200, '04/21/2013')
    insert into #tab1 values (1011, 105,'Tim',500, '06/18/2013')
    insert into #tab1 values (1012, 100,'Tom',800, '08/22/2013')
    insert into #tab1 values (1013, 101,'Bat',550, '09/15/2013')
    insert into #tab1 values (1014, 100,'Pet',200, '02/21/2013')
    insert into #tab1 values (1015, 103,'Vat',150, '03/18/2013')
    insert into #tab1 values (1016, 110,'Sun',800, '03/22/2013')
    insert into #tab1 values (1017, 111,'Bet',550, '12/15/2013')
    insert into #tab1 values (9999, 111,'AAA',110, '12/15/2014')
    create table #tab2 (IssueKey int, totalOutstanding numeric, sharedBenefits varchar(1)  )
    --drop table #tab2
    insert into #tab2 values (1000,  500,  'V')
    insert into #tab2 values (1001,  150,  'U')
    insert into #tab2 values (1002,  100,  'N')
    insert into #tab2 values (1003,  170,  'U')
    insert into #tab2 values (1010,  100,  'U')
    insert into #tab2 values (1011,  200,  'K')
    insert into #tab2 values (1012,  340,  'U')
    insert into #tab2 values (1013,  560,  'N')
    insert into #tab2 values (1014,  280,  'V')
    insert into #tab2 values (1015,  150,  'V')
    insert into #tab2 values (1016,  840,  'V')
    insert into #tab2 values (1017,  530,  'N')
    go
    WITH joined AS (
      SELECT a.MasterKey, a.AcctNo, a.name, a.SumaofShares,
             year(a.request_dat) AS year, b.totalOutstanding, b.sharedBenefits
      FROM   #tab1 a
      LEFT   JOIN #tab2 b ON a.MasterKey = b.IssueKey
    ), categories (category, sumofshares, totaloutstanding) AS (
       SELECT CASE WHEN A.MasterKey IS NOT NULL AND
                        B.MasterKey IS NOT NULL
                        THEN '2012 and 2013 accts ' +
                             CASE B.sharedBenefits
                                WHEN 'U' THEN 'UN Veriverted'
                                WHEN 'V' THEN 'Veriverted'
                                WHEN 'N' THEN 'NONVERTED'
                                ELSE ''
                             END
                   WHEN A.MasterKey IS NOT NULL THEN '2012 last year accounts'
                   WHEN B.MasterKey IS NOT NULL THEN '2013 this year accounts'
              END,
              coalesce(A.SumaofShares, 0) + coalesce(B.SumaofShares, 0),
              coalesce(A.totalOutstanding, 0) + coalesce(B.totalOutstanding, 0)
       FROM   (SELECT * FROM joined WHERE year = 2012) AS A
       FULL   JOIN (SELECT * FROM joined WHERE year = 2013) AS B
             ON A.AcctNo = B.AcctNo
            AND A.name   = B.name
    SELECT category, SUM(sumofshares) AS sumofshares,
           SUM(totaloutstanding) AS totaloutstanding
    FROM   categories
    GROUP  BY category
    go
    drop table #tab1, #tab2
    Erland Sommarskog, SQL Server MVP, [email protected]

  • How to find first and last date of a fiscal week using SQL

    Hello,
    I want information about FISCAL Week, means a Week based on ISO standard. I know format strings ‘IW’ or ‘IYYY’ gives fiscal week and fiscal year respectively from a given date. But I want to find the first and last date of a fiscal week. Say suppose I have a fiscal week is 2, and fiscal year is 2008, how to find the start and end date of the given fiscal week.
    Any kind of help would be greatly appreciable.
    Thanks,
    Prince

    davide gislon wrote:
    The following query evaluate the begin of a fisical week, where &year and &week are respectively the year and week you want to calculate.
    To evaluate the end of the week you have to add 6.
    Note that my database is set to have monday as day number 1 of the week, and sunday as day number 7; if your database settings are different you should modify the query accordingly.
    SELECT CASE TO_CHAR(TO_DATE('&year','YYYY'),'D')
    WHEN '1' THEN TO_DATE('&year','YYYY')+((&week-1)*7)
    WHEN '2' THEN TO_DATE('&year','YYYY')+((&week-1)*7-1)
    WHEN '3' THEN TO_DATE('&year','YYYY')+((&week-1)*7-2)
    WHEN '4' THEN TO_DATE('&year','YYYY')+((&week-1)*7-3)
    WHEN '5' THEN TO_DATE('&year','YYYY')+((&week-1)*7+3)
    WHEN '6' THEN TO_DATE('&year','YYYY')+((&week-1)*7+2)
    WHEN '7' THEN TO_DATE('&year','YYYY')+((&week-1)*7+1)
    END BEGIN_FISICAL_WEEK
    FROM DUAL
    Hope this is helpful.
    Cheers,
    Davide
    Edited by: davide gislon on 08-Jan-2009 07:19Your query does nothing you say it does. TO_DATE('&year','YYYY') returns first day of the current month for year &year. And the only reason it returns January 1, &year is that we are currently in January:
    SQL> select TO_DATE('&year','YYYY') from dual
      2  /
    Enter value for year: 2005
    old   1: select TO_DATE('&year','YYYY') from dual
    new   1: select TO_DATE('2005','YYYY') from dual
    TO_DATE('
    01-JAN-05
    SQL> As soon as we roll into February:
    SQL> alter system set fixed_date = '2009-2-1' scope=memory
      2  /
    System altered.
    SQL> select sysdate from dual
      2  /
    SYSDATE
    01-FEB-09
    SQL> select TO_DATE('&year','YYYY') from dual
      2  /
    Enter value for year: 2005
    old   1: select TO_DATE('&year','YYYY') from dual
    new   1: select TO_DATE('2005','YYYY') from dual
    TO_DATE('
    01-FEB-05
    SQL> alter system set fixed_date = NONE scope=both
      2  /
    System altered.
    SQL> select sysdate from dual
      2  /
    SYSDATE
    08-JAN-09
    SQL> But even if TO_DATE('&year','YYYY') would always return January 1, &year, or you would fix it to TO_DATE('0101&year','MMDDYYYY') it still would be wrong. ISO week rules are
    If January 1 falls on a Friday, Saturday, or Sunday, then the week including January 1 is the last week of the previous year, because most of the days in the week belong to the previous year.
    If January 1 falls on a Monday, Tuesday, Wednesday, or Thursday, then the week is the first week of the new year, because most of the days in the week belong to the new year.Therefore, next year:
    SQL> DEFINE YEAR=2010
    SQL> DEFINE WEEK=1
    SQL> ALTER SESSION SET NLS_TERRITORY=GERMANY -- enforce Monday as first day of the week
      2  /
    Session altered.
    SQL> SET VERIFY OFF
    SQL> SELECT CASE TO_CHAR(TO_DATE('0101&&year','MMDDYYYY'),'D')
      2  WHEN '1' THEN TO_DATE('0101&&year','MMDDYYYY')+((&&week-1)*7)
      3  WHEN '2' THEN TO_DATE('0101&&year','MMDDYYYY')+((&&week-1)*7-1)
      4  WHEN '3' THEN TO_DATE('0101&&year','MMDDYYYY')+((&&week-1)*7-2)
      5  WHEN '4' THEN TO_DATE('0101&&year','MMDDYYYY')+((&&week-1)*7-3)
      6  WHEN '5' THEN TO_DATE('0101&&year','MMDDYYYY')+((&&week-1)*7+3)
      7  WHEN '6' THEN TO_DATE('0101&&year','MMDDYYYY')+((&&week-1)*7+2)
      8  WHEN '7' THEN TO_DATE('0101&&year','MMDDYYYY')+((&&week-1)*7+1)
      9  END BEGIN_FISICAL_WEEK
    10  FROM DUAL
    11  /
    BEGIN_FI
    04.01.10
    SQL> SELECT TRUNC(TO_DATE('0101&&year','MMDDYYYY'),'IW') FROM DUAL
      2  /
    TRUNC(TO
    28.12.09
    SQL> 2 user10772980:
    Use:
    SELECT  TRUNC(TO_DATE('0101&&year','MMDDYYYY'),'IW') + (&&week-1)*7 FISCAL_YEAR_&&YEAR._WEEK_&&WEEK
      FROM  DUAL
    FISCAL_YEAR_2010_WEEK_1
    28.12.09
    SQL> SY.

  • How to calculate difference and percentage in matrix report dynamic columns in SSRS 2008

    Hi Friends,
    I need to calculate Difference and Percentage In matrix report SSRS 2008
    1) Row grouping based On Product
    2) Column Grouping Week and Fiscal Year Current week  and Previous Year same Weeks 
    3)Data Invnetory(Value fileld)
    Example:
    If value1 and Value2 Available we need to calculate Percentage and Difference.Otherwise Not required NA I need to Show.
    For example If Particular week,Current year data is available,Previous year  week data is not available No
    need to cal Diff and Percentage we need to show Empty.
    Below Code is working for If two values is available.
    If any one of the fiscal year week data is not there it's taking some garbage values.
    I tries with If condition in GetPCT() and GetDiff() It's not working.
    Can some one please help me on this.
    I am using the custom code 
    Public Shared Value1 as String
    Public Shared Value2 as String
    Public Shared previous as string
    Public Shared previousweek as string
    Public Shared Function GetValue(Value as String,partner as String,Week as String) as String
    If partner =previous and Week =previousweek Then
    Value2=Value
    Else
    previous=partner
    previousweek=Week
    Value1=Value
    End If
    return Value
    End Function
    Public Shared Function GetDiff()
    return Value2-Value1
    End Function
    Public Shared Function GetPct()
    return (Value2-Value1)/Value1
    End Function
    How to handle this.
    Thank You, Manasa.V

    Hi veerapaneni,
    According to your description, if one of the fiscal year week doesn’t have data, the custom code returns false results.
    For your requirement, if NULL value exists in the database, we should replace it as zero then perform calculate. So within the code, we should judge whether the value is NULL. To achieve your goal, please refer to the steps below:
    1. Create a table like below.
    create table dif4
    ([Product Group] varchar(50),
    [Week] varchar(50),
    [Fiscalyearweek] varchar(50),
    value int
    insert into dif4 values
    ('Desktops','W01','FY14W01',0),
    ('Desktops','W01','FY15W01',45),
    ('Desktops','W02','FY14W02',null),
    ('Desktops','W02','FY15W02',30),
    ('Desktops','W03','FY14W03',12),
    ('Desktops','W03','FY15W03',50),
    ('Notebooks','W01','FY14W01',35),
    ('Notebooks','W01','FY15W01',56),
    ('Notebooks','W02','FY14W02',45),
    ('Notebooks','W02','FY15W02',87),
    ('Notebooks','W03','FY14W03',75),
    ('Notebooks','W03','FY15W03',105),
    ('Tablets','W01','FY14W01',34),
    ('Tablets','W01','FY15W01',46),
    ('Tablets','W02','FY14W02',49),
    ('Tablets','W02','FY15W02',96),
    ('Tablets','W03','FY14W03',42),
    ('Tablets','W03','FY15W03',113)
    2. Add the custom code in the Report Properties.
    Public Shared Value1 as Integer
    Public Shared Value2 as Integer
    Public Shared previous as string
    Public Shared previousweek as string
    Public Shared Function GetValue(Value as Integer,product as String,Week as String) as Integer
    If Value=Nothing Then
    Value=0
    End If
    If product =previous and Week =previousweek Then
    Value2=Value
    Else
    previous=product
    previousweek=Week
    Value1=Value
    End If
    return Value
    End Function
    Public Shared Function GetPct()
    If Value1=0 Then
    return 0
    Else
    return (Value2-Value1)/Value1
    End If
    End Function
    Public Shared Function GetDiff()
    return Value2-Value1
    End Function
    3. Design the matrix like below.
    4. Then get the expected results.<o:p></o:p>
    If you have any question, please feel free to ask.
    Best regards,
    Qiuyun Yu
    Qiuyun Yu
    TechNet Community Support

  • How to find first and last day of last month?.

    Hello,
    I am using Crystal Report XI.
    Reports will be generated on first of every month for
    previous month. I want to find start and end date of previous month
    dynamic.
    For august, Start date is July 1st and End date July 31st.
    For September, start date is Aug 1st and Aug 31st.
    How can i get first of previous month as start date and last
    day of the previous month as end date?.
    Same kind of thing I want to do for future report ..find next
    month start date and end date.
    Thanks

    Adomacro,
    you could do a function, like this
    <cffunction name="getReportdates" returntype="struct"
    hint="Given an arbitrary month and year, the function returns a
    structure containing the first and last day of the previous
    month">
    <!--- Take current month and current year as the default
    --->
    <cfargument name="mnth" type="string" required="No"
    default="#monthAsString(month(now()))#">
    <cfargument name="yr" type="numeric" required="No"
    default="#year(now())#">
    <cfset firstDayOfGivenMonth = parseDateTime("1 " &
    arguments.mnth & " #arguments.yr#")>
    <cfset lastDayOfPreviousMonth =
    dateAdd("d",-1,firstDayOfGivenMonth)>
    <cfset numberOfDaysOfPreviousMonth =
    daysInMonth(lastDayOfPreviousMonth)>
    <cfset firstDayOfPreviousMonth =
    dateAdd("d",-#numberOfDaysOfPreviousMonth#,firstDayOfGivenMonth)>
    <cfset reportDates = structNew()>
    <cfset reportDates.firstDayOfPreviousMonth =
    firstDayOfPreviousMonth>
    <cfset reportDates.lastDayOfPreviousMonth =
    lastDayOfPreviousMonth>
    <cfreturn reportDates>
    </cffunction>
    <!--- Example usage --->
    <p>
    Given month: <strong>March
    2004</strong><br>
    begin date:
    <cfoutput>#dateFormat(getReportdates('March',2004).firstDayOfPreviousMonth,"d
    mmm yyyy")#</cfoutput><br>
    end date:
    <cfoutput>#dateFormat(getReportdates('March',2004).lastDayOfPreviousMonth,"d
    mmm yyyy")#</cfoutput><br>
    </p>
    <p>
    Given month: <strong>August
    2007</strong><br>
    begin date:
    <cfoutput>#dateFormat(getReportdates('August',2007).firstDayOfPreviousMonth,"d
    mmm yyyy")#</cfoutput><br>
    end date:
    <cfoutput>#dateFormat(getReportdates('August',2007).lastDayOfPreviousMonth,"d
    mmm yyyy")#</cfoutput><br>
    </p>

Maybe you are looking for