<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: FiscalYear Start Period Is not Correct in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/fiscalyear-start-period-is-not-correct/m-p/7249#M3168</link>
    <description>&lt;P&gt;Hi @Jason Johnson​&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you for posting your question in our community! We are happy to assist you.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Help us build a vibrant and resourceful community by recognizing and highlighting insightful contributions. Mark the best answers and show your appreciation!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 27 Mar 2023 05:15:22 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2023-03-27T05:15:22Z</dc:date>
    <item>
      <title>FiscalYear Start Period Is not Correct</title>
      <link>https://community.databricks.com/t5/data-engineering/fiscalyear-start-period-is-not-correct/m-p/7246#M3165</link>
      <description>&lt;P&gt;Hi, I'm trying to create a calendar dimension including a fiscal year with a fiscal start of April 1. I'm using the fiscalyear library and am setting the start to month 4 but it insists on setting April to month 7.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;runtime 12.1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My code snipet is:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;start_date = '2016-01-01'
    end_date = date(date.today().year, 12, 31) + relativedelta(years=5)
    
    fiscalyear.setup_fiscal_calendar(start_month=4)
    
    df = spark.sql(f"SELECT SEQUENCE(TO_TIMESTAMP('{start_date}'), TO_TIMESTAMP('{end_date}'), INTERVAL 1 DAY) AS DATETIME").withColumn("DATETIME", F.explode(F.col("DATETIME")))
&amp;nbsp;
    # bulk definition of UDFs
    udf_f_y = udf(lambda CELL: fiscalyear.FiscalDateTime(CELL.year, CELL.month, CELL.day).fiscal_year) # fiscal DateTime object f_y
    udf_f_m = udf(lambda CELL: fiscalyear.FiscalDateTime(CELL.year, CELL.month, CELL.day).fiscal_month) # fiscal DateTime object f_m
    udf_f_m_str = udf(lambda CELL: 'M' + str(CELL).zfill(2)) # use zfill to turn 2 into 02 and attach "M" to the front
    udf_f_q = udf(lambda CELL: fiscalyear.FiscalDateTime(CELL.year, CELL.month, CELL.day).fiscal_quarter) # fiscal DateTime object f_q
    udf_f_d = udf(lambda CELL: fiscalyear.FiscalDateTime(CELL.year, CELL.month, CELL.day).fiscal_day) # fiscal DateTime object f_d
&amp;nbsp;
    # Fiscal Columns
    
    df = df.withColumn("FISCAL_YEAR_INTEGER", udf_f_y(F.col("DATETIME")))
    df = df.withColumn("FISCAL_QUARTER_INTEGER",  udf_f_q(F.col("DATETIME")))
    df = df.withColumn("FISCAL_MONTH_INTEGER", udf_f_m(F.col("DATETIME")))
&amp;nbsp;
    df = df.withColumn("FISCAL_DAY_INTEGER", udf_f_d(F.col("DATETIME")))
    df = df.withColumn("FISCAL_YEAR_STRING", F.concat_ws("", F.lit("FY"),  F.col("FISCAL_YEAR_INTEGER").cast("STRING").substr(3, 4)))
    df = df.withColumn("FISCAL_QUARTER_STRING", F.concat_ws("_", F.col("FISCAL_YEAR_STRING"), F.concat_ws("", F.lit( "Q" ), F.col("FISCAL_QUARTER_INTEGER")) ) )
    df = df.withColumn("FISCAL_MONTH_STRING",  F.concat_ws("_", F.col("FISCAL_YEAR_STRING"), udf_f_m_str(F.col("FISCAL_MONTH_INTEGER"))))&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Not sure if it's something to do with using the udf and the lambda cell because I've tested it manually with the following and I properly get April as fiscal month 1:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;import fiscalyear
from pyspark.sql.functions import col
from pyspark.sql.functions import to_timestamp
&amp;nbsp;
#### Testing fiscalyear library #####
&amp;nbsp;
# Set the fiscal year start month to April (month 4)
fiscalyear.START_MONTH = 4
&amp;nbsp;
# # Create a FiscalDateTime object representing a calendar date
df = fiscalyear.FiscalDateTime(2023, 4, 1)
&amp;nbsp;
# Convert the calendar date to a fiscal year
fiscal_year = df.fiscal_year
fiscal_month = df.fiscal_month
&amp;nbsp;
print(fiscal_year, fiscal_month)
&amp;nbsp;
2024 1&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Can anyone offer any insights? Thank you for looking.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Mar 2023 20:17:37 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/fiscalyear-start-period-is-not-correct/m-p/7246#M3165</guid>
      <dc:creator>JJ_LVS1</dc:creator>
      <dc:date>2023-03-22T20:17:37Z</dc:date>
    </item>
    <item>
      <title>Re: FiscalYear Start Period Is not Correct</title>
      <link>https://community.databricks.com/t5/data-engineering/fiscalyear-start-period-is-not-correct/m-p/7247#M3166</link>
      <description>&lt;P&gt;@Jason Johnson​&amp;nbsp;:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It seems that the fiscalyear library you are using is actually setting the start month to July (month 7) instead of April (month 4) as you intended. This might be a bug or a known issue with the library.&lt;/P&gt;&lt;P&gt;To work around this, you can manually adjust the month of your date inputs to compensate for the offset. For example, you can subtract 3 from the month of your start date to shift it back to April:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;start_date = '2016-01-01'
start_date = f'{start_date[:5]}{int(start_date[5:7])-3:02d}{start_date[7:]}'
&amp;nbsp;
# start_date is now '2016-04-01'&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Similarly, you can subtract 3 from the month of your fiscal year calculation to get the correct fiscal month:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;udf_f_m = udf(lambda CELL: fiscalyear.FiscalDateTime(CELL.year, CELL.month, CELL.day).fiscal_month - 3)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;With these adjustments, your code should correctly generate a calendar dimension with a fiscal year starting in April.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2023 04:41:05 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/fiscalyear-start-period-is-not-correct/m-p/7247#M3166</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-03-23T04:41:05Z</dc:date>
    </item>
    <item>
      <title>Re: FiscalYear Start Period Is not Correct</title>
      <link>https://community.databricks.com/t5/data-engineering/fiscalyear-start-period-is-not-correct/m-p/7248#M3167</link>
      <description>&lt;P&gt;Thanks for the reply.  Not sure why I hadn't thought of doing this.  I'll add these changes in and see how it turns out.  &lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2023 14:06:10 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/fiscalyear-start-period-is-not-correct/m-p/7248#M3167</guid>
      <dc:creator>JJ_LVS1</dc:creator>
      <dc:date>2023-03-23T14:06:10Z</dc:date>
    </item>
    <item>
      <title>Re: FiscalYear Start Period Is not Correct</title>
      <link>https://community.databricks.com/t5/data-engineering/fiscalyear-start-period-is-not-correct/m-p/7249#M3168</link>
      <description>&lt;P&gt;Hi @Jason Johnson​&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you for posting your question in our community! We are happy to assist you.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Help us build a vibrant and resourceful community by recognizing and highlighting insightful contributions. Mark the best answers and show your appreciation!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Mar 2023 05:15:22 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/fiscalyear-start-period-is-not-correct/m-p/7249#M3168</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-03-27T05:15:22Z</dc:date>
    </item>
    <item>
      <title>Re: FiscalYear Start Period Is not Correct</title>
      <link>https://community.databricks.com/t5/data-engineering/fiscalyear-start-period-is-not-correct/m-p/58429#M31137</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import fiscalyear
import datetime

def get_fiscal_date(year,month,day):
  fiscalyear.setup_fiscal_calendar(start_month=4)
  v_fiscal_month=fiscalyear.FiscalDateTime(year, month, day).fiscal_month #To get the Fiscal Month
  v_fiscal_quarter=fiscalyear.FiscalDateTime(year, month, day).fiscal_quarter #To get the Fiscal Month
  v_fiscal_year=fiscalyear.FiscalDateTime(year, month, day).fiscal_year #To get the Fiscal Month
  return v_fiscal_month,v_fiscal_quarter,v_fiscal_year


from pyspark.sql.functions import udf,col
from pyspark.sql.types import ArrayType, IntegerType,StringType

# Define the UDF with return type of ArrayType containing three IntegerTypes
get_fiscal_date_udf = udf(lambda year, month, day: get_fiscal_date(year, month, day), ArrayType(StringType()))

# Register the UDF with Spark SQL
spark.udf.register("get_fiscal_date_udf", get_fiscal_date,(ArrayType(StringType())))

%sql
select get_fiscal_date_udf(2024,4,24), get_fiscal_date_udf(2023,12,24), get_fiscal_date_udf(2024,1,24), get_fiscal_date_udf(2024,8,24)&lt;/LI-CODE&gt;&lt;P&gt;In My case, April is the Financial start month, so in UDF function, we have to set&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;fiscalyear.setup_fiscal_calendar(&lt;/SPAN&gt;&lt;SPAN&gt;start_month&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;4&lt;/SPAN&gt;&lt;SPAN&gt;).&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;But in ur case ur calling directly main function, by default its taking in build start month&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jan 2024 15:56:40 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/fiscalyear-start-period-is-not-correct/m-p/58429#M31137</guid>
      <dc:creator>DataEnginner</dc:creator>
      <dc:date>2024-01-25T15:56:40Z</dc:date>
    </item>
  </channel>
</rss>

