Jaspersoft BI Tools

iReport,Jaspersoft Design Studio,Jaspersoft Server, Schema Workbench,Analysis,Dashboards, Self Service BI, Domains,Topics,Ad-Hoc Reports, Jaspersoft ETL, Embedding BI,HTML-5 charts, Fusion Charts,JFree Charts,Drill down reports , Drill down Dashboards,Visualize.js, Performance Tuning, D3 Charts, Jaspersoft Customization

Monday, June 27, 2016

Tip : Table Component Columns hiding based on Date Parameter in Jasper Reports (Dates Comparision in Jasper Reports)

Hi All,

In this post you will see a use case associated with Table Component in Japser Reports.

Use Case : 
Lets assume you have a table component that has 24 columns(months data).
Lets assume you have a parameter called "param_StartDate" =  "15-10-2015".
Lets assume there are 12 columns for "Unit Sales" and 12 columns for "Store Sales" alternatively.

The table component has to conditionally display "Store Sales" till last month and it has to display only "Unit Sales" starting from the current month.

i.e.,

Column1= Oct 2015 Unit Sales
Column2= Oct 2015 Store Sales

Column3 = Nov 2015 Unit Sales
Column4= Nov 2015 Store Sales

Column5= Dec 2015 Unit Sales
Column6= Dec 2015 Store Sales

Column7= Jan 2016 Unit Sales
Column8 = Jan 2016 Store Sales

Column9= Feb 2016 Unit Sales
Column10=Feb 2016 Store Sales

Column11= Feb 2016 Unit Sales
Column12=Feb 2016 Store Sales

-----------------------------------------
Column13= Mar 2015 Unit Sales
Column14= Mar 2015 Store Sales

Column15 = Apr 2015 Unit Sales
Column16= Apr 2015 Store Sales

Column17= May 2015 Unit Sales
Column18= May 2015 Store Sales

Column19= June 2016 Unit Sales ( Current Month)

Column21= Feb 2016 Unit Sales

Column23= Feb 2016 Unit Sales



Column22 and Column24 should be not displayed  as those falls in Current Month (June 2015) and next month.



Parameters Set : 
param_StartDate(java.util.Date)
Parameter1 = $P{param_StartDate} (java.util.Date)
Parameter2 = new Date() (java.util.Date)

Add below package in Report "Import" properties
<import value="org.apache.commons.lang.time.*"/>

NOTE: DateUtils and its methods are written in above package.

In the table component column headers write below expressions.

$P{Parameter1},0).getYear() = Current Month
$P{Parameter1},1).getYear() = Next Month
$P{Parameter1},2).getYear() = Next, next month.

Column1 Print When Expression on 1st Cell :

((DateUtils.addMonths($P{Parameter1},0).getYear()+1900)<($P{Parameter2}.getYear()+1900))
||
(
    (DateUtils.addMonths($P{Parameter1},0).getYear()+1900)==($P{Parameter2}.getYear()+1900)
    &&
    (DateUtils.addMonths($P{Parameter1},0).getMonth()+1)<($P{Parameter2}.getMonth()+1)
)


Column2 Print When Expression on 2nd Cell : 
((DateUtils.addMonths($P{Parameter1},1).getYear()+1900)<($P{Parameter2}.getYear()+1900))
||
(
    (DateUtils.addMonths($P{Parameter1},1).getYear()+1900)==($P{Parameter2}.getYear()+1900)
    &&
    (DateUtils.addMonths($P{Parameter1},1).getMonth()+1)<($P{Parameter2}.getMonth()+1)
)



 Column3 Print When Expression on 3rd Cell : 
((DateUtils.addMonths($P{Parameter1},2).getYear()+1900)<($P{Parameter2}.getYear()+1900))
||
(
    (DateUtils.addMonths($P{Parameter1},2).getYear()+1900)==($P{Parameter2}.getYear()+1900)
    &&
    (DateUtils.addMonths($P{Parameter1},2).getMonth()+1)<($P{Parameter2}.getMonth()+1)
)


Using  these expressions one can hide the columns on table component.


:-)